Passed
Push — master ( b67a30...b7883b )
by Kevin
09:53 queued 08:10
created

EmailExtension::__toString()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 12
ccs 6
cts 6
cp 1
rs 10
cc 2
nc 2
nop 0
crap 2
1
<?php
2
3
namespace Zenstruck\ScheduleBundle\Schedule\Extension;
4
5
use Symfony\Component\Mailer\MailerInterface;
6
use Symfony\Component\Mime\Address;
7
use Symfony\Component\Mime\Email;
8
use Zenstruck\ScheduleBundle\Schedule\Extension;
9
10
/**
11
 * @author Kevin Bond <[email protected]>
12
 */
13
final class EmailExtension implements Extension, HasMissingHandlerMessage
14
{
15
    private $hook;
16
    private $email;
17
18
    /**
19
     * @param string|Address|string[]|Address[]|null $to
20
     */
21 11
    private function __construct(string $hook, $to = null, string $subject = null, callable $callback = null)
22
    {
23 11
        if (!\interface_exists(MailerInterface::class)) {
24
            throw new \LogicException(\sprintf('Symfony Mailer is required to use the "%s" extension. Install with "composer require symfony/mailer".', self::class));
25
        }
26
27 11
        $this->hook = $hook;
28
29 11
        $email = new Email();
30
31 11
        if (null !== $to) {
32 6
            $email->to(...(array) $to);
33
        }
34
35 11
        if ($subject) {
36 4
            $email->subject($subject);
37
        }
38
39 11
        if ($callback) {
40 4
            $callback($email);
41
        }
42
43 11
        $this->email = $email;
44 11
    }
45
46 3
    public function __toString(): string
47
    {
48 3
        $to = $this->email->getTo();
49
50 3
        if (empty($to)) {
51 1
            return "{$this->hook}, email output";
52
        }
53
54
        $to = \array_map(function (Address $address) { return $address->toString(); }, $to);
55 2
        $to = \implode('; ', $to);
56
57 2
        return "{$this->hook}, email output to \"{$to}\"";
58
    }
59
60 3
    public static function taskAfter($to = null, string $subject = null, callable $callback = null): self
61
    {
62 3
        return new self(Extension::TASK_AFTER, $to, $subject, $callback);
63
    }
64
65 5
    public static function taskFailure($to = null, string $subject = null, callable $callback = null): self
66
    {
67 5
        return new self(Extension::TASK_FAILURE, $to, $subject, $callback);
68
    }
69
70 5
    public static function scheduleFailure($to = null, string $subject = null, callable $callback = null): self
71
    {
72 5
        return new self(Extension::SCHEDULE_FAILURE, $to, $subject, $callback);
73
    }
74
75 8
    public function getEmail(): Email
76
    {
77 8
        return $this->email;
78
    }
79
80 8
    public function isHook(string $expectedHook): bool
81
    {
82 8
        return $expectedHook === $this->hook;
83
    }
84
85 3
    public function getMissingHandlerMessage(): string
86
    {
87 3
        return 'To use the email extension you must configure a mailer (config path: "zenstruck_schedule.email_handler").';
88
    }
89
}
90