MailExecutor::send()   F
last analyzed

Complexity

Conditions 16
Paths 8192

Size

Total Lines 61
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 26
CRAP Score 18.4426

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 16
eloc 35
nc 8192
nop 2
dl 0
loc 61
ccs 26
cts 33
cp 0.7879
crap 18.4426
rs 1.4
c 1
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Kaliop\eZMigrationBundle\Core\Executor;
4
5
use Kaliop\eZMigrationBundle\API\EmbeddedReferenceResolverBagInterface;
6
use Kaliop\eZMigrationBundle\API\Exception\InvalidStepDefinitionException;
7
use Kaliop\eZMigrationBundle\API\ReferenceResolverInterface;
8
use Kaliop\eZMigrationBundle\API\Value\MigrationStep;
9
use Swift_Message;
10
use Swift_Attachment;
11
12
/**
13
 * @property EmbeddedReferenceResolverBagInterface $referenceResolver
14
 */
15
class MailExecutor extends AbstractExecutor
16
{
17
    use IgnorableStepExecutorTrait;
0 ignored issues
show
Bug introduced by
The trait Kaliop\eZMigrationBundle...orableStepExecutorTrait requires the property $dsl which is not provided by Kaliop\eZMigrationBundle...e\Executor\MailExecutor.
Loading history...
18
19
    protected $supportedStepTypes = array('mail');
20
    protected $supportedActions = array('send');
21
22
    protected $mailService;
23
24
    /**
25
     * MailExecutor constructor.
26
     * @param $mailService
27
     * @param EmbeddedReferenceResolverBagInterface $referenceResolver must implement EmbeddedReferenceResolverInterface too
28 149
     */
29
    public function __construct($mailService, EmbeddedReferenceResolverBagInterface $referenceResolver)
30 149
    {
31 149
        $this->mailService = $mailService;
32 149
        $this->referenceResolver = $referenceResolver;
33
    }
34
35
    /**
36
     * @param MigrationStep $step
37
     * @return mixed
38
     * @throws \Exception
39 1
     */
40
    public function execute(MigrationStep $step)
41 1
    {
42
        parent::execute($step);
43 1
44
        if (!isset($step->dsl['mode'])) {
45
            throw new InvalidStepDefinitionException("Invalid step definition: missing 'mode'");
46
        }
47 1
48
        $action = $step->dsl['mode'];
49 1
50
        if (!in_array($action, $this->supportedActions)) {
51
            throw new InvalidStepDefinitionException("Invalid step definition: value '$action' is not allowed for 'mode'");
52
        }
53 1
54
        $this->skipStepIfNeeded($step);
55 1
56
        return $this->$action($step->dsl, $step->context);
57
    }
58
59
    /**
60
     * @param array $dsl
61
     * @param array $context
62
     * @return true
63
     * @throws \Exception
64 1
     */
65
    protected function send($dsl, $context)
66
    {
67 1
        // cater to Swiftmailer 5 and 6
68
        if (is_callable(array('Swift_Message', 'newInstance'))) {
69
            $message = Swift_Message::newInstance();
70 1
        } else {
71
            $message = new Swift_Message();
72
        }
73 1
74 1
        if (isset($dsl['from'])) {
75
            $message->setFrom($this->resolveReferencesRecursively($dsl['from']));
76 1
        }
77 1
        if (isset($dsl['to'])) {
78
            $message->setTo($this->resolveReferencesRecursively($dsl['to']));
79 1
        }
80 1
        if (isset($dsl['cc'])) {
81
            $message->setCc($this->resolveReferencesRecursively($dsl['cc']));
82 1
        }
83 1
        if (isset($dsl['bcc'])) {
84
            $message->setBcc($this->resolveReferencesRecursively($dsl['bcc']));
85 1
        }
86 1
        if (isset($dsl['subject'])) {
87
            $message->setSubject($this->resolveReferencesInText($dsl['subject']));
88 1
        }
89 1
        if (isset($dsl['body'])) {
90
            $message->setBody($this->resolveReferencesInText($dsl['body']));
91 1
        }
92 1
        if (isset($dsl['attach'])) {
93
            $paths = $this->resolveReferencesRecursively($dsl['attach']);
94 1
            foreach((array)$paths as $path) {
95 1
                // we use the same logic as for the image/file fields in content: look up file 1st relative to the migration
96
                $attachment = dirname($context['path']) . '/' . $path;
97
                if (!is_file($attachment)) {
98 1
                    $attachment = $path;
99
                }
100
                $message->attach(Swift_Attachment::fromPath($attachment));
101 1
            }
102
        }
103
104 1
        if (isset($dsl['priority'])) {
105
            $message->setPriority($this->resolveReference($dsl['priority']));
106
        }
107 1
        if (isset($dsl['read_receipt_to'])) {
108
            $message->setReadReceiptTo($this->resolveReferencesRecursively($dsl['read_receipt_to']));
109
        }
110 1
        if (isset($dsl['return_path'])) {
111
            $message->setReturnPath($this->resolveReferencesRecursively($dsl['return_path']));
0 ignored issues
show
Bug introduced by
It seems like $this->resolveReferences...ly($dsl['return_path']) can also be of type array and array; however, parameter $address of Swift_Mime_SimpleMessage::setReturnPath() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

111
            $message->setReturnPath(/** @scrutinizer ignore-type */ $this->resolveReferencesRecursively($dsl['return_path']));
Loading history...
112
        }
113 1
        if (isset($dsl['reply_to'])) {
114
            $message->setReplyTo($this->resolveReferencesRecursively($dsl['reply_to']));
115
        }
116
        if (isset($dsl['sender'])) {
117 1
            $message->setSender($this->resolveReferencesRecursively($dsl['sender']));
0 ignored issues
show
Bug introduced by
It seems like $this->resolveReferences...rsively($dsl['sender']) can also be of type array and array; however, parameter $address of Swift_Mime_SimpleMessage::setSender() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

117
            $message->setSender(/** @scrutinizer ignore-type */ $this->resolveReferencesRecursively($dsl['sender']));
Loading history...
118
        }
119
120
        $this->mailService->send($message);
121
122 1
        // q: shall we set any reference?
123
124
        // q: what to return?
125
        return true;
126
    }
127
128 1
    /**
129
     * Replaces any references inside a string
130 1
     *
131 1
     * @param string
132 1
     * @return string
133
     * @throws \Exception
134 1
     */
135
    protected function resolveReferencesInText($text)
136 1
    {
137
        return $this->referenceResolver->resolveEmbeddedReferences($text);
138
    }
139
}
140