Completed
Push — master ( 2a6a78...c2b2ef )
by DELEVOYE
05:24
created

MessageManager   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 78
Duplicated Lines 8.97 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 93.94%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 8
dl 7
loc 78
ccs 31
cts 33
cp 0.9394
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A send() 7 20 2
B getBody() 0 20 6

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
/*
4
 * This file is part of the MindbazBundle package.
5
 *
6
 * (c) David DELEVOYE <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Kozikaza\MindbazBundle\Manager;
13
14
use Kozikaza\MindbazBundle\Exception\SendErrorException;
15
use Kozikaza\MindbazBundle\Model\Subscriber;
16
use mbzOneshot\OneshotWebService;
17
use mbzOneshot\Send;
18
use Psr\Log\LoggerInterface;
19
20
/**
21
 * @author Vincent Chalamon <[email protected]>
22
 */
23
class MessageManager
24
{
25
    const MINDBAZ_SEND_RESPONSE_OK = 'OK';
26
    const MINDBAZ_SEND_RESPONSE_NOK = 'NOK';
27
28
    /**
29
     * @var OneshotWebService
30
     */
31
    private $oneshotWebService;
32
33
    /**
34
     * @var LoggerInterface
35
     */
36
    private $logger;
37
38
    /**
39
     * @param OneshotWebService $oneshotWebService
40
     * @param LoggerInterface   $logger
41
     */
42 3
    public function __construct(OneshotWebService $oneshotWebService, LoggerInterface $logger)
43
    {
44 3
        $this->oneshotWebService = $oneshotWebService;
45 3
        $this->logger = $logger;
46 3
    }
47
48
    /**
49
     * @param int                 $idCampaign
50
     * @param Subscriber          $subscriber
51
     * @param \Swift_Mime_Message $message
52
     */
53 2
    public function send($idCampaign, Subscriber $subscriber, \Swift_Mime_Message $message)
54
    {
55 2
        $response = $this->oneshotWebService->Send(
56 2
            new Send(
57 2
                $idCampaign,
58 2
                $subscriber->getId(),
59 2
                $this->getBody($message, 'text/html'),
60 2
                $this->getBody($message, 'text/plain'),
61 2
                $message->getSender(),
62 2
                $message->getSubject()
63 2
            )
64 2
        );
65 2 View Code Duplication
        if (self::MINDBAZ_SEND_RESPONSE_OK === $response->getSendResult()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
66 1
            $this->logger->info('Message successfully sent to subscriber', ['id' => $subscriber->getId()]);
67 1
        } else {
68 1
            $this->logger->error('An error occurred while sending the message to subscriber', ['id' => $subscriber->getId(), 'response' => $response->getSendResult()]);
69
70 1
            throw new SendErrorException();
71
        }
72 1
    }
73
74
    /**
75
     * @param \Swift_Mime_Message $message
76
     * @param string              $contentTypeRequired
77
     *
78
     * @return null|string
79
     */
80 2
    private function getBody(\Swift_Mime_Message $message, $contentTypeRequired)
81
    {
82 2
        $contentType = $message->getContentType();
83
84 2
        if ('multipart/alternative' === $contentType) {
85
            $contentType = $message->getBody() !== strip_tags($message->getBody()) ? 'text/html' : 'text/plain';
86
        }
87
88 2
        if ($contentTypeRequired === $contentType) {
89 2
            return $message->getBody();
90
        }
91
92 2
        foreach ($message->getChildren() as $child) {
93 1
            if ($contentTypeRequired === $child->getContentType()) {
94 1
                return $child->getBody();
95
            }
96 1
        }
97
98 1
        return null;
99
    }
100
}
101