GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Branch develop (5ddd6a)
by Borut
04:53 queued 02:04
created

Mailer::swiftMessageInitializeAndSend()   C

Complexity

Conditions 10
Paths 82

Size

Total Lines 67
Code Lines 40

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 67
rs 6.1506
cc 10
eloc 40
nc 82
nop 1

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 Application;
4
5
use Silex\Application;
6
7
/**
8
 * @author Borut Balažek <[email protected]>
9
 */
10
class Mailer
11
{
12
    protected $app;
13
14
    protected $swiftMessageInstance;
15
    protected $swiftMessageInstanceTemplate;
16
17
    /**
18
     * @param Silex\Application $app
19
     */
20
    public function __construct(Application $app)
21
    {
22
        $this->app = $app;
23
    }
24
25
    /**
26
     * Prepares the (swift) email and sends it.
27
     *
28
     * @return integer
29
     */
30
    public function swiftMessageInitializeAndSend(array $data = array())
31
    {
32
        $swiftMessageInstance = \Swift_Message::newInstance();
33
        
34
        if (!isset($data['subject'])) {
35
            throw new Exception('You need to specify a subject');
36
        }
37
        
38
        if (!isset($data['to'])) {
39
            throw new Exception('You need to specify a recipient');
40
        }
41
        
42
        $from = isset($data['from'])
43
            ? $data['from']
44
            : array($this->app['email'] => $this->app['emailName'])
45
        ;
46
        $to = $data['to'];
47
        
48
        $swiftMessageInstance
49
            ->setSubject($data['subject'])
50
            ->setTo($to)
51
            ->setFrom($from)
52
        ;
53
54
        if (isset($data['cc'])) {
55
            $swiftMessageInstance->setCc($data['cc']);
56
        }
57
58
        if (isset($data['bcc'])) {
59
            $swiftMessageInstance->setBcc($data['bcc']);
60
        }
61
        
62
        $templateData = array(
63
            'app' => $this->app,
64
            'user' => $this->app['user'],
65
            'email' => $to,
66
            'swiftMessage' => $swiftMessageInstance,
67
        );
68
69
        if (isset($data['templateData'])) {
70
            $templateData = array_merge(
71
                $templateData,
72
                $data['templateData']
73
            );
74
        }
75
76
        if (isset($data['body'])) {
77
            $bodyType = isset($data['bodyType'])
78
                ? $data['bodyType']
79
                : 'text/html'
80
            ;
81
            $isTwigTemplate = isset($data['contentIsTwigTemplate'])
82
                ? $data['contentIsTwigTemplate']
83
                : true
84
            ;
85
86
            $swiftMessageBody = $this->app['mailer.css_to_inline_styles_converter'](
87
                $data['body'],
88
                $templateData,
89
                $isTwigTemplate
90
            );
91
92
            $swiftMessageInstance->setBody($swiftMessageBody, $bodyType);
93
        }
94
95
        return $this->app['mailer']->send($swiftMessageInstance);
96
    }
97
98
    /***** Swift Message Instance *****/
99
    public function getSwiftMessageInstance()
100
    {
101
        return $this->swiftMessageInstance;
102
    }
103
104
    public function setSwiftMessageInstance(\Swift_Message $swiftMessageInstance)
105
    {
106
        $this->swiftMessageInstance = $swiftMessageInstance;
107
108
        return $this;
109
    }
110
111
    /**
112
     * Sends the (swift) email
113
     */
114
    public function send($swiftMessage = false)
115
    {
116
        if (!$swiftMessage) {
117
            $swiftMessage = $this->getSwiftMessageInstance();
118
        }
119
120
        return $this->app['mailer']->send($swiftMessage);
121
    }
122
123
    /**
124
     * Short for swift image
125
     */
126
    public function image($path)
127
    {
128
        return \Swift_Image::fromPath($path);
129
    }
130
}
131