RecordProvider   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 144
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 12

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 10
c 2
b 1
f 0
lcom 1
cbo 12
dl 0
loc 144
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getProvider() 0 4 1
A getProviderParameters() 0 4 1
A createNew() 0 4 1
A dispatchEvent() 0 4 1
A __construct() 0 12 1
A findTransactionId() 0 8 1
B storeResult() 0 25 1
A visit() 0 18 3
1
<?php
2
3
namespace DoS\SMSBundle\Provider;
4
5
use Doctrine\ORM\EntityManager;
6
use DoS\ResourceBundle\Doctrine\ORM\EntityRepository;
7
use DoS\ResourceBundle\Factory\ResourceFactoryAware;
8
use DoS\SMSBundle\Model\RecordInterface;
9
use DoS\SMSBundle\SMS\ProviderInterface;
10
use libphonenumber\PhoneNumberUtil;
11
use SmsSender\Result\ResultInterface;
12
use Symfony\Component\EventDispatcher\Event;
13
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
14
use Symfony\Component\EventDispatcher\GenericEvent;
15
16
class RecordProvider extends ResourceFactoryAware
17
{
18
    /**
19
     * @var ProviderProvider
20
     */
21
    protected $provider;
22
23
    /**
24
     * @var EventDispatcherInterface
25
     */
26
    protected $eventDispatcher;
27
28
    /**
29
     * @var EntityManager
30
     */
31
    protected $manager;
32
33
    /**
34
     * @var EntityRepository
35
     */
36
    protected $repository;
37
38
    /**
39
     * @var string
40
     */
41
    protected $dataClass;
42
43
    public function __construct(
44
        EventDispatcherInterface $eventDispatcher,
45
        ProviderProvider $provider,
46
        EntityManager $manager,
0 ignored issues
show
Bug introduced by
You have injected the EntityManager via parameter $manager. This is generally not recommended as it might get closed and become unusable. Instead, it is recommended to inject the ManagerRegistry and retrieve the EntityManager via getManager() each time you need it.

The EntityManager might become unusable for example if a transaction is rolled back and it gets closed. Let’s assume that somewhere in your application, or in a third-party library, there is code such as the following:

function someFunction(ManagerRegistry $registry) {
    $em = $registry->getManager();
    $em->getConnection()->beginTransaction();
    try {
        // Do something.
        $em->getConnection()->commit();
    } catch (\Exception $ex) {
        $em->getConnection()->rollback();
        $em->close();

        throw $ex;
    }
}

If that code throws an exception and the EntityManager is closed. Any other code which depends on the same instance of the EntityManager during this request will fail.

On the other hand, if you instead inject the ManagerRegistry, the getManager() method guarantees that you will always get a usable manager instance.

Loading history...
47
        $dataClass
48
    ) {
49
        $this->eventDispatcher = $eventDispatcher;
50
        $this->provider = $provider;
51
        $this->manager = $manager;
52
        $this->dataClass = $dataClass;
53
        $this->repository = $manager->getRepository($dataClass);
54
    }
55
56
    /**
57
     * @return ProviderProvider
58
     */
59
    public function getProvider()
60
    {
61
        return $this->provider;
62
    }
63
64
    /**
65
     * @param $name
66
     *
67
     * @return array
68
     */
69
    public function getProviderParameters($name)
70
    {
71
        return $this->provider->getParameters($name);
72
    }
73
74
    /**
75
     * @return RecordInterface
76
     */
77
    public function createNew()
78
    {
79
        return $this->factory->createNew();
80
    }
81
82
    /**
83
     * @param $transactionId
84
     *
85
     * @return null|RecordInterface
86
     */
87
    public function findTransactionId($transactionId)
88
    {
89
        return $this->repository->findOneBy(
90
            array(
91
                'transactionId' => $transactionId,
92
            )
93
        );
94
    }
95
96
    /**
97
     * @param ResultInterface $result
98
     *
99
     * @return RecordInterface
100
     */
101
    public function storeResult(ResultInterface $result)
102
    {
103
        $number = PhoneNumberUtil::getInstance()->parse($result->getRecipient(), 'TH');
104
        $provider = $this->provider->getActivedProvider();
105
        $object = $this->createNew();
106
107
        $object->setMessage($result->getBody());
108
        $object->setTransactionId($result->getId());
109
        $object->setNumber($number);
110
        $object->setState($object::STATE_SENT);
111
        $object->setPrice($provider->getPrice());
112
        $object->setProvider($provider);
113
        $object->setCurrency($provider->getCurrency());
114
115
        $event = new GenericEvent($object);
116
117
        $this->dispatchEvent('dos_sms_record_pre_store', $event);
118
119
        $this->manager->persist($object);
120
        $this->manager->flush();
121
122
        $this->dispatchEvent('dos_sms_record_post_store', $event);
123
124
        return $object;
125
    }
126
127
    /**
128
     * @param string $name
129
     * @param Event  $event
130
     *
131
     * @return Event
132
     */
133
    public function dispatchEvent($name, Event $event)
134
    {
135
        return $this->eventDispatcher->dispatch($name, $event);
136
    }
137
138
    /**
139
     * @param ProviderInterface $provider
140
     */
141
    public function visit(ProviderInterface $provider)
142
    {
143
        foreach ($provider->getCallbackResults() as $result) {
144
            if ($record = $this->findTransactionId($result->getMessageId())) {
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $record is correct as $this->findTransactionId($result->getMessageId()) (which targets DoS\SMSBundle\Provider\R...er::findTransactionId()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
145
                $record->responded($result->getData(), $result->isSuccess());
146
                $record->setPrice($result->getPrice());
147
148
                $event = new GenericEvent($record);
149
150
                $this->dispatchEvent('dos_sms_record_pre_response', $event);
151
152
                $this->manager->persist($record);
153
                $this->manager->flush();
154
155
                $this->dispatchEvent('dos_sms_record_post_response', $event);
156
            }
157
        }
158
    }
159
}
160