Completed
Push — master ( b322d1...651cee )
by Axel
8s
created

FeatureManager::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1.008

Importance

Changes 2
Bugs 0 Features 2
Metric Value
c 2
b 0
f 2
dl 0
loc 5
ccs 4
cts 5
cp 0.8
rs 9.4285
cc 1
eloc 4
nc 1
nop 3
crap 1.008
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
use Developtech\AgilityBundle\Utils\Slugger;
13
14
class FeatureManager {
15
    /** @var EntityManager **/
16
    protected $em;
17
    /** @var Slugger **/
18
    protected $slugger;
19
    /** @var string **/
20
    protected $featureClass;
21
22
    /**
23
     * @param EntityManager $em
24
     * @param string $featureClass
25
     */
26 4
    public function __construct(EntityManager $em, Slugger $slugger, $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...
27 4
        $this->em = $em;
28 4
        $this->slugger = $slugger;
29 4
        $this->featureClass = $featureClass;
30
    }
31
32
    /**
33
     * @param ProjectModel $project
34
     * @return array
35
     */
36 1
    public function getProjectFeatures(ProjectModel $project) {
37
        return $this->em->getRepository($this->featureClass)->findByProject($project);
38 1
    }
39
40
    /**
41
     * @param integer $id
42
     * @return FeatureModel
43
     */
44 1
    public function getFeature($id) {
45
        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...
46
            throw new NotFoundHttpException('Feature not found');
47
        }
48 1
        return $feature;
49
    }
50
51
    /**
52
    *  @param ProjectModel $project
53
     * @param string $name
54
     * @param string $description
55
     * @param integer $status
56
     * @param integer $productOwnerValue
57
     * @param UserInterface $developer
58
     * @return FeatureModel
59
     */
60 1
    public function createProductOwnerFeature(ProjectModel $project, $name, $description, $status, $productOwnerValue = null, $developer = null) {
61
        $feature =
62
            (new $this->featureClass())
63 1
            ->setProject($project)
64 1
            ->setType('product-owner')
65 1
            ->setName($name)
66
            ->setSlug($this->slugger->slugify($name))
67 1
            ->setDescription($description)
68 1
            ->setStatus($status)
69 1
            ->setProductOwnerValue($productOwnerValue)
70
            ->setDeveloper($developer)
71 1
        ;
72
        $project->addFeature($feature);
73
        $this->em->persist($feature);
74
        $this->em->flush();
75 1
        return $feature;
76 1
    }
77
}
78