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

MessageManager::getBody()   B

Complexity

Conditions 6
Paths 12

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 6.1666

Importance

Changes 0
Metric Value
dl 0
loc 20
ccs 10
cts 12
cp 0.8333
rs 8.8571
c 0
b 0
f 0
cc 6
eloc 10
nc 12
nop 2
crap 6.1666
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