ProviderProvider::getParameters()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
1
<?php
2
3
namespace DoS\SMSBundle\Provider;
4
5
use Doctrine\ORM\EntityManager;
6
use DoS\ResourceBundle\Factory\ResourceFactoryAware;
7
use DoS\SMSBundle\Model\ProviderInterface;
8
use Sylius\Component\Resource\Repository\RepositoryInterface;
9
10
class ProviderProvider extends ResourceFactoryAware
11
{
12
    /**
13
     * @var EntityManager
14
     */
15
    protected $manager;
16
17
    /**
18
     * @var RepositoryInterface
19
     */
20
    protected $repository;
21
22
    /**
23
     * @var string
24
     */
25
    protected $dataClass;
26
27
    /**
28
     * @var string
29
     */
30
    protected $defaultProvider;
31
32
    public function __construct(EntityManager $manager, $dataClass, $defaultProvider = 'dummy')
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...
33
    {
34
        $this->manager = $manager;
35
        $this->dataClass = $dataClass;
36
        $this->repository = $manager->getRepository($dataClass);
0 ignored issues
show
Documentation Bug introduced by
It seems like $manager->getRepository($dataClass) of type object<Doctrine\ORM\EntityRepository> is incompatible with the declared type object<Sylius\Component\...ry\RepositoryInterface> of property $repository.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
37
        $this->defaultProvider = $defaultProvider;
38
    }
39
40
    /**
41
     * @param $name
42
     */
43
    public function setDefaultProvider($name)
44
    {
45
        $this->defaultProvider = $name;
46
    }
47
48
    /**
49
     * @param $name
50
     *
51
     * @return null|ProviderInterface
52
     */
53
    public function findByName($name)
54
    {
55
        return $this->repository->findOneBy(array('name' => $name));
56
    }
57
58
    /**
59
     * @return null|ProviderInterface
60
     */
61
    public function getActivedProvider()
62
    {
63
        if ($provider = $this->repository->findOneBy(array('actived' => true))) {
64
            return $provider;
65
        }
66
67
        return $this->findByName($this->defaultProvider);
68
    }
69
70
    /**
71
     * Get paramter by provider name.
72
     *
73
     * @param $name
74
     *
75
     * @return array
76
     */
77
    public function getParameters($name)
78
    {
79
        if (!$provider = $this->findByName($name)) {
80
            return $provider->getParameters();
81
        }
82
83
        return array();
84
    }
85
}
86