Jak vyřešit dědičnost construktoru

jAkErCZ
Člen | 321
+
0
-

Čau,
řeším tu problém ze kterého už fakt nevím jak vybruslit mám funkci Loader na který mám nasměrovaný commandy pro cron.

<?php

namespace App\CoreModule\Model;

use Nette\Database\Context;

/**
 * Description of Loader
 *
 * @author jAk3r
 */
class Loader
{
    /** @var Context */
    private $connection;

    public function __construct(Context $connection)
    {
        $this->connection = $connection;
    }

    /**
     * @param $message
     * @param $source
     * @param $typ
     */
    public function log($message, $source, $typ){
        $this->connection->table('logs')->insert([
            'date' => new \DateTime('now'),
            'message' => $message,
            'source' => $source,
            'typ' => $typ
        ]);
    }

    public function fenixOfferSend()
    {
        $fenix = new MailFunction($this->connection);
        $fenix->fenixOfferMail();
    }

    public function cronMail1()
    {
        $fenix = new MailFunction($this->connection);
        $fenix->sendSystemMail_1();
    }

    public function cronMail2()
    {
        $fenix = new MailFunction($this->connection);
        $fenix->sendSystemMail_2();
    }

}

Chyba nastává v tom že jakmile se zvolá určitá funkce tak mi hází problém s dědičností construktoru…

<?php

namespace App\CoreModule\Model;


use GuzzleHttp\Client;
use Latte\Engine;
use Nette\Mail\IMailer;
use Nette\Database\Context;
use Nette\Utils\DateTime;
use GuzzleHttp\RequestOptions;
use Nette\Mail\Message;
use Nette\Mail\SendmailMailer;

/**
 * Description of MailFunction
 *
 * @author jAk3r
 */
class MailFunction {
    /** @var Context */
    private $connection;

    /** @var \Nette\Mail\IMailer @inject */
    public $mailer;

    public function __construct(Context $connection, IMailer $mailer)
    {
        $this->connection = $connection;
        $this->mailer = $mailer;
    }

Chyba je

Type error: Too few arguments to function App\CoreModule\Model\MailFunction::__construct(), 1 passed in C:\xampp\htdocs\Intranet\app\CoreModule\model\Loader.php on line 44 and exactly 2 expected

Jde o to že dokud jsem do contruktoru nepřidal IMailer tak to funguje ale zase mi to na funkci $this->mailer->send(); hází že je nulová… Takže jelikož potřebuji předat závislost tak jsem to přidal do contruktoru ale to teď zase hází tohle…

Díky

David Matějka
Moderator | 6445
+
0
-

tak kdyz tu instanci vytvaris rucne, tak tam musis vsechny argumenty predat. (a nevim, jak s tim souvisi dedicnost konstruktoru, tady zadnou dedicnost nemas)

a proc rovnou nemas zaregistrovane MailFunction jako sluzbu, kterou budes vyzadovat v konstruktoru tridy Loader?

jAkErCZ
Člen | 321
+
0
-

David Matějka napsal(a):

tak kdyz tu instanci vytvaris rucne, tak tam musis vsechny argumenty predat. (a nevim, jak s tim souvisi dedicnost konstruktoru, tady zadnou dedicnost nemas)

a proc rovnou nemas zaregistrovane MailFunction jako sluzbu, kterou budes vyzadovat v konstruktoru tridy Loader?

Myslíš takto že?

public $mailFunction;

 public function __construct(MailFunction $mailFunction)
    {
       $this->mailFunction = $mailFunction;
    }
jAkErCZ
Člen | 321
+
0
-

Tak začneme popořadě.

Command na konzoli

<?php

namespace App\Console;

use App\CoreModule\Model\Loader;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Nette\Database\Context;

class CronMail1 extends Command {

    /** @var Context @inject */
    public $connection;

    protected function configure() {
        $this->setName('cron:mail1')->setDescription('Odeslani mailu: 7 veci');
    }

    /**
     * @param InputInterface $input
     * @param OutputInterface $output
     * @return int|null
     */
    protected function execute(InputInterface $input, OutputInterface $output) {
        try {
            $loader = new Loader();
            $loader->cronMail1();
            $loader->log('[OK] - Maily: 7 Věcí byli odeslány', 'mail1','0');
            $output->writeLn('<info>[OK] - Data byli odeslany na fenix</info>');
            return 0; // zero return code means everything is ok
        } catch (\Exception $exc) {
            $loader = new Loader($this->connection);
            $loader->log('Chyba - ' . $exc->getMessage(),'mail1', '1');
            $output->writeLn('<error>Chyba - ' . $exc->getMessage() . '</error>');
            return 1; // non-zero return code means error
        }
    }

}

Loader.php

<?php

namespace App\CoreModule\Model;

use Nette\Database\Context;

/**
 * Description of Loader
 *
 * @author jAk3r
 */
class Loader
{
    /** @var Context */
    private $connection;

    /** @var MailFunction */
    public $mailFunction;

    public function __construct(Context $connection, MailFunction $mailFunction)
    {
        $this->connection = $connection;
        $this->mailFunction = $mailFunction;
    }

    /**
     * @param $message
     * @param $source
     * @param $typ
     */
    public function log($message, $source, $typ){
        $this->connection->table('logs')->insert([
            'date' => new \DateTime('now'),
            'message' => $message,
            'source' => $source,
            'typ' => $typ
        ]);
    }

    public function fenixOfferSend()
    {
        $this->mailFunction->fenixOfferMail();
    }

    public function cronMail1()
    {
        $this->mailFunction->sendSystemMail_1();
    }

    public function cronMail2()
    {
        $this->mailFunction->sendSystemMail_2();
    }

}

MailFunction.php

<?php

namespace App\CoreModule\Model;


use GuzzleHttp\Client;
use Latte\Engine;
use Nette\Mail\IMailer;
use Nette\Database\Context;
use Nette\Utils\DateTime;
use GuzzleHttp\RequestOptions;
use Nette\Mail\Message;
use Nette\Mail\SendmailMailer;

/**
 * Description of MailFunction
 *
 * @author jAk3r
 */
class MailFunction {
    /** @var Context */
    private $connection;

    /** @var \Nette\Mail\IMailer @inject */
    public $mailer;

    public function __construct(Context $connection, IMailer $mailer)
    {
        $this->connection = $connection;
        $this->mailer = $mailer;
    }

Jak se to dá jednoduše vyřešit abych nemusel v construktoru už od commandu předávat IMailer…

David Matějka
Moderator | 6445
+
+3
-
  • Loader registruj jako sluzbu
  • MailFunction registruj jako sluzbu
  • do commandu nepredavej Context, ale Loader jako zavislost pres inject
fejjo
Člen | 4
+
+1
-

Ahoj, neviem či to pomôže v tvojom probléme ale neviem prečo máš @inject pri tej property a zároveň máš v __construct IMailer. Podľa dokumentace https://doc.nette.org/…dependencies#… to pridáváš buď pomocou @inject alebo pomocou konstruktoru.

jAkErCZ
Člen | 321
+
0
-

David Matějka napsal(a):

  • Loader registruj jako sluzbu
  • MailFunction registruj jako sluzbu
  • do commandu nepredavej Context, ale Loader jako zavislost pres inject

Díky :)