Completed
Push — master ( cd6d5c...fa5688 )
by Axel
05:46 queued 03:12
created

FeatureManager   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 2
Metric Value
wmc 4
c 2
b 0
f 2
lcom 1
cbo 3
dl 0
loc 34
ccs 10
cts 10
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getProjectFeatures() 0 3 1
A getFeature() 0 6 2
1
<?php
2
3
namespace Developtech\AgilityBundle\Manager;
4
5
use Doctrine\ORM\EntityManager;
6
7
use Developtech\AgilityBundle\Model\ProjectModel;
8
use Developtech\AgilityBundle\Model\FeatureModel;
9
10
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
11
12
class FeatureManager {
13
    /** @var EntityManager **/
14
    protected $em;
15
    /** @var string **/
16
    protected $featureClass;
17
18
    /**
19
     * @param EntityManager $em
20
     * @param string $featureClass
21
     */
22 3
    public function __construct(EntityManager $em, $featureClass) {
0 ignored issues
show
Bug introduced by
You have injected the EntityManager via parameter $em. 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...
23 3
        $this->em = $em;
24 3
        $this->featureClass = $featureClass;
25 3
    }
26
27
    /**
28
     * @param ProjectModel $project
29
     * @return array
30
     */
31 1
    public function getProjectFeatures(ProjectModel $project) {
32 1
        return $this->em->getRepository($this->featureClass)->findByProject($project);
33
    }
34
35
    /**
36
     * @param integer $id
37
     * @return FeatureModel
38
     */
39 2
    public function getFeature($id) {
40 2
        if(($feature = $this->em->getRepository($this->featureClass)->find($id)) === null) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after IF keyword; 0 found
Loading history...
41 1
            throw new NotFoundHttpException('Feature not found');
42
        }
43 1
        return $feature;
44
    }
45
}
46