Completed
Pull Request — develop (#39)
by Axel
03:13
created

FeatureManager::getFeature()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2.1481

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 6
ccs 2
cts 3
cp 0.6667
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
crap 2.1481
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 Developtech\AgilityBundle\Entity\Feature;
11
12
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
13
14
use Developtech\AgilityBundle\Utils\Slugger;
15
16
class FeatureManager {
17
    /** @var EntityManager **/
18
    protected $em;
19
    /** @var Slugger **/
20
    protected $slugger;
21
22
    /**
23
     * @param EntityManager $em
24
     * @param Slugger $slugger
25
     */
26 4
    public function __construct(EntityManager $em, Slugger $slugger) {
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
    }
30
31
    /**
32
     * @param ProjectModel $project
33
     * @return array
34
     */
35 1
    public function getProjectFeatures(ProjectModel $project) {
36
        return $this->em->getRepository(Feature::class)->findByProject($project);
37 1
    }
38
39
    /**
40
     * @param integer $id
41
     * @return FeatureModel
42
     */
43 1
    public function getFeature($id) {
44
        if(($feature = $this->em->getRepository(Feature::class)->find($id)) === null) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after IF keyword; 0 found
Loading history...
45
            throw new NotFoundHttpException('Feature not found');
46
        }
47 1
        return $feature;
48
    }
49
50
    /**
51
    *  @param ProjectModel $project
52
     * @param string $name
53
     * @param string $description
54
     * @param integer $status
55
     * @param integer $productOwnerValue
56
     * @param UserInterface $developer
57
     * @return FeatureModel
58
     */
59 1
    public function createProductOwnerFeature(ProjectModel $project, $name, $description, $status, $productOwnerValue = null, $developer = null) {
60
        $feature =
61
            (new Feature())
62 1
            ->setProject($project)
63 1
            ->setType('product-owner')
64 1
            ->setName($name)
65
            ->setSlug($this->slugger->slugify($name))
66 1
            ->setDescription($description)
67 1
            ->setStatus($status)
68 1
            ->setProductOwnerValue($productOwnerValue)
69
            ->setDeveloper($developer)
0 ignored issues
show
Bug introduced by
It seems like $developer defined by parameter $developer on line 59 can be null; however, Developtech\AgilityBundl...reModel::setDeveloper() does not accept null, maybe add an additional type check?

It seems like you allow that null is being passed for a parameter, however the function which is called does not seem to accept null.

We recommend to add an additional type check (or disallow null for the parameter):

function notNullable(stdClass $x) { }

// Unsafe
function withoutCheck(stdClass $x = null) {
    notNullable($x);
}

// Safe - Alternative 1: Adding Additional Type-Check
function withCheck(stdClass $x = null) {
    if ($x instanceof stdClass) {
        notNullable($x);
    }
}

// Safe - Alternative 2: Changing Parameter
function withNonNullableParam(stdClass $x) {
    notNullable($x);
}
Loading history...
70 1
        ;
71
        $project->addFeature($feature);
72
        $this->em->persist($feature);
73
        $this->em->flush();
74 1
        return $feature;
75 1
    }
76
}
77