]> git.agnieray.net Git - galette.git/commitdiff
Improve Mailing constructor
authorJohan Cwiklinski <johan@x-tnd.be>
Sun, 18 Oct 2020 10:13:44 +0000 (12:13 +0200)
committerJohan Cwiklinski <johan@x-tnd.be>
Sun, 18 Oct 2020 12:26:45 +0000 (14:26 +0200)
Drop a global usage; refs #414
Fix mailing calls

15 files changed:
galette/includes/dependencies.php
galette/lib/Galette/Controllers/Crud/MailingsController.php
galette/lib/Galette/Controllers/HistoryController.php
galette/lib/Galette/Core/History.php
galette/lib/Galette/Core/Mailing.php
galette/lib/Galette/Core/MailingHistory.php
tests/Galette/Core/tests/units/History.php
tests/Galette/Core/tests/units/Links.php
tests/Galette/Core/tests/units/Login.php
tests/Galette/Entity/tests/units/Adherent.php
tests/Galette/Entity/tests/units/Contribution.php
tests/Galette/IO/tests/units/CsvIn.php
tests/Galette/Repository/tests/units/Members.php
tests/Galette/Repository/tests/units/Reminders.php
tests/Galette/Util/tests/units/Password.php

index e56259491a99359dbabf030778ee43b996ec180d..1497be4c6a8a0b09cc215bca24773ac4beb85235 100644 (file)
@@ -228,7 +228,7 @@ $container['print_logo'] = function ($c) {
 
 
 $container['history'] = function ($c) {
-    return new Galette\Core\History($c->get('zdb'), $c->get('login'));
+    return new Galette\Core\History($c->get('zdb'), $c->get('login'), $c->get('preferences'));
 };
 
 $container['acls'] = function ($c) {
index bfa7ae9af5b8738ef92a718fb79f4bcb44cbde0a..5e0e0702c3e7ebd837df66c58d373a1c4e639180 100644 (file)
@@ -123,7 +123,7 @@ class MailingsController extends CrudController
             ) {
                 $mailing = $this->session->mailing;
             } elseif (isset($get['from']) && is_numeric($get['from'])) {
-                $mailing = new Mailing($this->preferences, null, $get['from']);
+                $mailing = new Mailing($this->preferences, [], (int)$get['from']);
                 MailingHistory::loadFrom($this->zdb, (int)$get['from'], $mailing);
             } elseif (isset($get['reminder'])) {
                 //FIXME: use a constant!
@@ -132,7 +132,7 @@ class MailingsController extends CrudController
                 $filters->filter_account = Members::ACTIVE_ACCOUNT;
                 $m = new Members($filters);
                 $members = $m->getList(true);
-                $mailing = new Mailing($this->preferences, ($members !== false) ? $members : null);
+                $mailing = new Mailing($this->preferences, ($members !== false) ? $members : []);
             } else {
                 if (
                     count($filters->selected) == 0
@@ -162,7 +162,7 @@ class MailingsController extends CrudController
                 }
                 $m = new Members();
                 $members = $m->getArrayList($filters->selected);
-                $mailing = new Mailing($this->preferences, ($members !== false) ? $members : null);
+                $mailing = new Mailing($this->preferences, ($members !== false) ? $members : []);
             }
 
             if (isset($get['remove_attachment'])) {
@@ -707,7 +707,7 @@ class MailingsController extends CrudController
 
         $mailing = null;
         if (isset($args['id'])) {
-            $mailing = new Mailing($this->preferences, null);
+            $mailing = new Mailing($this->preferences);
             MailingHistory::loadFrom($this->zdb, (int)$args['id'], $mailing, false);
             $attachments = $mailing->attachments;
         } else {
@@ -769,7 +769,7 @@ class MailingsController extends CrudController
      */
     public function previewAttachment(Request $request, Response $response, array $args = []): Response
     {
-        $mailing = new Mailing($this->preferences, null);
+        $mailing = new Mailing($this->preferences);
         MailingHistory::loadFrom($this->zdb, (int)$args['id'], $mailing, false);
         $attachments = $mailing->attachments;
         $attachment = $attachments[$args['pos']];
index 41bf309391e1cbdbe6514d3c4f0817a654e2113f..06a7e8b40357657329c7a43bfee31fd3e03773f2 100644 (file)
@@ -214,7 +214,7 @@ class HistoryController extends AbstractController
             try {
                 $this->history->clean();
                 //reinitialize object after flush
-                $this->history = new History($this->zdb, $this->login);
+                $this->history = new History($this->zdb, $this->login, $this->preferences);
                 $filters = new HistoryList();
                 $this->session->filter_history = $filters;
 
@@ -251,7 +251,7 @@ class HistoryController extends AbstractController
     }
 
     /**
-     * History flush rconfirmation
+     * History flush confirmation
      *
      * @param Request  $request  PSR Request
      * @param Response $response PSR Response
index 9b87e073d641eb279bbafb6b1755f1b082f488b6..c80fea9bfe410a27396ed71b4cda3a7453742049 100644 (file)
@@ -63,6 +63,7 @@ class History
     protected $count;
     protected $zdb;
     protected $login;
+    protected $preferences;
     protected $filters;
 
     protected $users;
@@ -73,14 +74,16 @@ class History
     /**
      * Default constructor
      *
-     * @param Db          $zdb     Database
-     * @param Login       $login   Login
-     * @param HistoryList $filters Filtering
+     * @param Db          $zdb         Database
+     * @param Login       $login       Login
+     * @param Preferences $preferences Preferences
+     * @param HistoryList $filters     Filtering
      */
-    public function __construct(Db $zdb, Login $login, $filters = null)
+    public function __construct(Db $zdb, Login $login, Preferences $preferences, $filters = null)
     {
         $this->zdb = $zdb;
         $this->login = $login;
+        $this->preferences = $preferences;
 
         if ($filters === null) {
             $this->filters = new HistoryList();
@@ -122,9 +125,7 @@ class History
      */
     public function add($action, $argument = '', $query = '')
     {
-        global $preferences;
-
-        if ($preferences->pref_log == Preferences::LOG_DISABLED) {
+        if ($this->preferences->pref_log == Preferences::LOG_DISABLED) {
             //logs are disabled
             return true;
         }
index 995a0950f96c903123dcb3ef3598524c9ed28506..bbbe7c45ea168229c99f6e0e46efd9006f33b63b 100644 (file)
@@ -81,19 +81,16 @@ class Mailing extends GaletteMail
      * @param array       $members     An array of members
      * @param int         $id          Identifier, defaults to null
      */
-    public function __construct(Preferences $preferences, $members, $id = null)
+    public function __construct(Preferences $preferences, array $members = [], int $id = null)
     {
         parent::__construct($preferences);
-        if ($id !== null) {
-            $this->id = $id;
-        } else {
-            $this->generateNewId();
-        }
+        $this->id = $id ?? $this->generateNewId();
+
         $this->current_step = self::STEP_START;
         $this->mime_type = self::MIME_DEFAULT;
         /** TODO: add a preference that propose default mime-type to use,
             then init it here */
-        if ($members !== null) {
+        if (count($members)) {
             //Check which members have a valid email address and which have not
             $this->setRecipients($members);
         }
@@ -103,15 +100,16 @@ class Mailing extends GaletteMail
     /**
      * Generate new mailing id and temporary path
      *
-     * @return void
+     * @return integer
      */
-    private function generateNewId()
+    private function generateNewId(): int
     {
         global $zdb;
 
         $pass = new Password($zdb);
         $this->id = $pass->makeRandomPassword(30);
         $this->generateTmpPath($this->id);
+        return $this->id;
     }
 
     /**
index 1a0a581f949c430ad65d2f72c722a607c6a15991..65941739e164ad714f3a1277541b3d7e352b02f5 100644 (file)
@@ -80,24 +80,16 @@ class MailingHistory extends History
     /**
      * Default constructor
      *
-     * @param Db          $zdb     Database
-     * @param Login       $login   Login
-     * @param HistoryList $filters Filtering
-     * @param Mailing     $mailing Mailing
+     * @param Db                $zdb         Database
+     * @param Login             $login       Login
+     * @param Preferences       $preferences Preferences
+     * @param MailingsList|null $filters     Filtering
+     * @param Mailing|null      $mailing     Mailing
      */
-    public function __construct(Db $zdb, Login $login, $filters = null, $mailing = null)
+    public function __construct(Db $zdb, Login $login, Preferences $preferences, MailingsList $filters = null, Mailing $mailing = null)
     {
-        parent::__construct($zdb, $login, $filters);
-
-        if ($mailing instanceof Mailing) {
-            $this->mailing = $mailing;
-        } elseif ($mailing !== null) {
-            Analog::log(
-                '[' . __METHOD__ .
-                '] Mailing should be either null or an instance of Mailing',
-                Analog::ERROR
-            );
-        }
+        parent::__construct($zdb, $login, $preferences, $filters);
+        $this->mailing = $mailing;
     }
 
     /**
@@ -550,7 +542,7 @@ class MailingHistory extends History
         if (is_array($list)) {
             try {
                 foreach ($list as $id) {
-                    $mailing = new Mailing($preferences, null, $id);
+                    $mailing = new Mailing($preferences, [], $id);
                     $mailing->removeAttachments();
                 }
 
index 88f6955d9ef32d014775204ffe7176ce48edb27f..e17adc9085273bd6694bc157823e377bcb5ae503 100644 (file)
@@ -58,6 +58,7 @@ class History extends atoum
     private $zdb;
     private $i18n;
     private $session;
+    private $preferences;
 
     /**
      * Set up tests
@@ -72,7 +73,10 @@ class History extends atoum
         $this->i18n = new \Galette\Core\I18n();
         $this->session = new \RKA\Session();
         $this->login = new \Galette\Core\Login($this->zdb, $this->i18n, $this->session);
-        $this->history = new \Galette\Core\History($this->zdb, $this->login);
+        $this->preferences = new \Galette\Core\Preferences(
+            $this->zdb
+        );
+        $this->history = new \Galette\Core\History($this->zdb, $this->login, $this->preferences);
     }
 
     /**
@@ -163,7 +167,7 @@ class History extends atoum
             throw new \LogicException('Error executing query!', 123);
         };
 
-        $this->history = new \Galette\Core\History($this->zdb, $this->login);
+        $this->history = new \Galette\Core\History($this->zdb, $this->login, $this->preferences);
         $add = $this->history->add('Test');
         $this->boolean($add)->isFalse();
     }
@@ -180,7 +184,7 @@ class History extends atoum
             throw new \LogicException('Error executing query!', 123);
         };
 
-        $this->history = new \Galette\Core\History($this->zdb, $this->login);
+        $this->history = new \Galette\Core\History($this->zdb, $this->login, $this->preferences);
         $list = $this->history->getHistory();
         $this->boolean($list)->isFalse();
     }
index 198554bd93d698a8c386cfcfb29b3201bbd85d07..f5dddb52e1b68a137529df4bfb156eb707e13dd0 100644 (file)
@@ -104,7 +104,7 @@ class Links extends atoum
         );
         $this->session = new \RKA\Session();
         $this->login = new \Galette\Core\Login($this->zdb, $this->i18n, $this->session);
-        $this->history = new \Galette\Core\History($this->zdb, $this->login);
+        $this->history = new \Galette\Core\History($this->zdb, $this->login, $this->preferences);
 
         global $zdb, $login, $hist, $i18n; // globals :(
         $zdb = $this->zdb;
index 6b72dd7b3a417f8cfbc2840d86c85d9b26e0a0fa..3f6328ed95744a3ac83434437b8b8c81b1fb53cc 100644 (file)
@@ -95,7 +95,7 @@ class Login extends atoum
         $this->preferences = new \Galette\Core\Preferences(
             $this->zdb
         );
-        $this->history = new \Galette\Core\History($this->zdb, $this->login);
+        $this->history = new \Galette\Core\History($this->zdb, $this->login, $this->preferences);
 
         include_once GALETTE_ROOT . 'includes/fields_defs/members_fields.php';
         $this->members_fields = $members_fields;
index 65806a02344eb21accbd39d3f6d7f8d6de89da6b..f948f15a5890dd6dd4cc3d8af087d2ded31ff0fa 100644 (file)
@@ -120,7 +120,7 @@ class Adherent extends atoum
         );
         $this->session = new \RKA\Session();
         $this->login = new \Galette\Core\Login($this->zdb, $this->i18n, $this->session);
-        $this->history = new \Galette\Core\History($this->zdb, $this->login);
+        $this->history = new \Galette\Core\History($this->zdb, $this->login, $this->preferences);
 
         $this->default_deps = [
             'picture'   => true,
index 8a8f56eaf69dd721f575a130e0e228d9064c9488..cc4dfcff19068a431575b98cf98fb21bf0a8ca97 100644 (file)
@@ -120,7 +120,7 @@ class Contribution extends atoum
         );
         $this->session = new \RKA\Session();
         $this->login = new \Galette\Core\Login($this->zdb, $this->i18n, $this->session);
-        $this->history = new \Galette\Core\History($this->zdb, $this->login);
+        $this->history = new \Galette\Core\History($this->zdb, $this->login, $this->preferences);
 
         global $zdb, $login, $hist, $i18n; // globals :(
         $zdb = $this->zdb;
index 1bf8c1be915a6cd80bacbbf321afda1e1331ceb6..293fa778c3f5b4c893056746de616af2930cc752 100644 (file)
@@ -96,7 +96,7 @@ class CsvIn extends atoum
         );
         $this->session = new \RKA\Session();
         $this->login = new \Galette\Core\Login($this->zdb, $this->i18n, $this->session);
-        $this->history = new \Galette\Core\History($this->zdb, $this->login);
+        $this->history = new \Galette\Core\History($this->zdb, $this->login, $this->preferences);
         $flash_data = [];
         $this->flash_data = &$flash_data;
         $this->flash = new \Slim\Flash\Messages($flash_data);
index f9f2390c8afc86268562d4d40a128b9b154efce0..0ee138a135e9d8d7b42d222c97838a164f58b90c 100644 (file)
@@ -81,7 +81,7 @@ class Members extends atoum
         );
         $this->session = new \RKA\Session();
         $this->login = new \Galette\Core\Login($this->zdb, $this->i18n, $this->session);
-        $this->history = new \Galette\Core\History($this->zdb, $this->login);
+        $this->history = new \Galette\Core\History($this->zdb, $this->login, $this->preferences);
 
         include_once GALETTE_ROOT . 'includes/fields_defs/members_fields.php';
         $this->members_fields = $members_fields;
index 449df41092ca6a221000bfed67553e295636f662..9e1eb6d24d0e42f942b80ba79ff784c030bf48d2 100644 (file)
@@ -102,7 +102,7 @@ class Reminders extends atoum
         );
         $this->session = new \RKA\Session();
         $this->login = new \Galette\Core\Login($this->zdb, $this->i18n, $this->session);
-        $this->history = new \Galette\Core\History($this->zdb, $this->login);
+        $this->history = new \Galette\Core\History($this->zdb, $this->login, $this->preferences);
 
         global $zdb, $login, $hist, $i18n; // globals :(
         $zdb = $this->zdb;
index 0498667ce4d3abccc12ba7b23ff8509a5e4c4d87..fe0e673474bd5ac0f052619ed43f645012b64d78 100644 (file)
@@ -253,7 +253,7 @@ class Password extends atoum
             \Galette\Core\I18n::DEFAULT_LANG
         );
         $login = new \Galette\Core\Login($this->zdb, $i18n, $session);
-        $history = new \Galette\Core\History($this->zdb, $login);
+        $history = new \Galette\Core\History($this->zdb, $login, $this->preferences);
         include_once GALETTE_ROOT . 'includes/fields_defs/members_fields.php';
         $members_fields = $members_fields;