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

SubscriberManager::findByEmail()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 21
ccs 14
cts 14
cp 1
rs 9.3142
c 0
b 0
f 0
cc 3
eloc 12
nc 3
nop 1
crap 3
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\Model\Subscriber;
15
use Kozikaza\MindbazBundle\Serializer\Bridge\Serializer;
16
use Kozikaza\MindbazBundle\Serializer\SubscriberEncoder;
17
use mbzSubscriber\ArrayOfInt;
18
use mbzSubscriber\ArrayOfString;
19
use mbzSubscriber\GetSubscribersByEmail;
20
use mbzSubscriber\InsertSubscriber;
21
use mbzSubscriber\Subscriber as MindbazSubscriber;
22
use mbzSubscriber\SubscriberWebService;
23
use mbzSubscriber\Unsubscribe;
24
use Psr\Log\LoggerInterface;
25
use Psr\Log\NullLogger;
26
27
/**
28
 * @author Vincent Chalamon <[email protected]>
29
 */
30
class SubscriberManager
31
{
32
    /**
33
     * @var SubscriberWebService
34
     */
35
    private $subscriberWebService;
36
37
    /**
38
     * @var Serializer
39
     */
40
    private $serializer;
41
42
    /**
43
     * @var LoggerInterface
44
     */
45
    private $logger;
46
47
    /**
48
     * @param SubscriberWebService $subscriberWebService
49
     * @param Serializer           $serializer
50
     * @param LoggerInterface|null $logger
51
     */
52 8
    public function __construct(SubscriberWebService $subscriberWebService, Serializer $serializer, LoggerInterface $logger = null)
53
    {
54 8
        $this->subscriberWebService = $subscriberWebService;
55 8
        $this->serializer = $serializer;
56 8
        $this->logger = $logger ?: new NullLogger();
57 8
    }
58
59
    /**
60
     * @param array $fields
61
     *
62
     * @return Subscriber
63
     */
64 1
    public function create(array $fields)
65
    {
66 1
        $subscriber = $this->serializer->denormalize($fields, Subscriber::class);
67 1
        $this->insert($subscriber);
68
69 1
        return $subscriber;
70
    }
71
72
    /**
73
     * @param Subscriber $subscriber
74
     */
75 1
    public function insert(Subscriber $subscriber)
76
    {
77
        /** @var MindbazSubscriber $mbzSubscriber */
78 1
        $mbzSubscriber = $this->serializer->serialize($subscriber, SubscriberEncoder::FORMAT);
79 1
        $result = $this->subscriberWebService->InsertSubscriber(new InsertSubscriber($mbzSubscriber, true));
80 1
        $subscriber->setId($result->getInsertSubscriberResult());
81
82 1
        $this->logger->info('New subscriber inserted in Mindbaz', ['id' => $subscriber->getId()]);
83 1
    }
84
85
    /**
86
     * @param Subscriber $subscriber
87
     */
88 2
    public function unsubscribe(Subscriber $subscriber)
89
    {
90 2
        $result = $this->subscriberWebService->Unsubscribe(new Unsubscribe($subscriber->getId(), null, null));
0 ignored issues
show
Documentation introduced by
null is of type null, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
91 2 View Code Duplication
        if (true === $result->getUnsubscribeResult()) {
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...
92 1
            $this->logger->info('Subscriber successfully unsubscribed', ['id' => $subscriber->getId()]);
93 1
        } else {
94 1
            $this->logger->error('An error occurred while unsubscribing subscriber', ['id' => $subscriber->getId(), 'response' => $result->getUnsubscribeResult()]);
95
        }
96 2
    }
97
98
    /**
99
     * @param array $emails
100
     *
101
     * @return array
102
     */
103 4
    public function findByEmail(array $emails)
104
    {
105 4
        $result = $this->subscriberWebService->GetSubscribersByEmail(
106 4
            new GetSubscribersByEmail(
107 4
                (new ArrayOfString())->setString(array_map('strtolower', $emails)),
108 4
                (new ArrayOfInt())->setInt([0, 1])
109 4
            )
110 4
        )->getGetSubscribersByEmailResult();
111
112
        // Unable to find related subscribers
113 4
        if (null === $result) {
114 2
            return [];
115
        }
116
117 2
        $subscribers = [];
118 2
        foreach ($result->getSubscriber() as $subscriber) {
119 2
            $subscribers[] = $this->serializer->deserialize($subscriber, Subscriber::class, SubscriberEncoder::FORMAT);
120 2
        }
121
122 2
        return $subscribers;
123
    }
124
125
    /**
126
     * @param string $email
127
     *
128
     * @return Subscriber|null
129
     */
130 2
    public function findOneByEmail($email)
131
    {
132 2
        $subscribers = $this->findByEmail([$email]);
133
134 2
        return 0 < count($subscribers) ? $subscribers[0] : null;
135
    }
136
}
137