Completed
Push — master ( 46961e...b2acd3 )
by Axel
04:17 queued 01:11
created

ProjectManager::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 2
Metric Value
c 2
b 0
f 2
dl 0
loc 4
ccs 4
cts 4
cp 1
rs 10
cc 1
eloc 3
nc 1
nop 2
crap 1
1
<?php
2
3
namespace Developtech\AgilityBundle\Manager;
4
5
use Doctrine\ORM\EntityManager;
6
7
use Developtech\AgilityBundle\Entity\Project;
8
9
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
10
11
use Symfony\Component\Security\Core\User\UserInterface;
12
13
use Developtech\AgilityBundle\Utils\Slugger;
14
15
class ProjectManager {
16
    /** @var Doctrine\ORM\EntityManager **/
17
    protected $em;
18
    /** @var Developtech\AgilityBundle\Utils\Slugger **/
19
    protected $slugger;
20
21
    /**
22
     * @param Doctrine\ORM\EntityManager $em
23
     * @param Slugger $slugger
24
     */
25 5
    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...
26 5
        $this->em = $em;
0 ignored issues
show
Documentation Bug introduced by
It seems like $em of type object<Doctrine\ORM\EntityManager> is incompatible with the declared type object<Developtech\Agili...rine\ORM\EntityManager> of property $em.

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...
27 5
        $this->slugger = $slugger;
0 ignored issues
show
Documentation Bug introduced by
It seems like $slugger of type object<Developtech\AgilityBundle\Utils\Slugger> is incompatible with the declared type object<Developtech\Agili...tyBundle\Utils\Slugger> of property $slugger.

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...
28 5
    }
29
30
    /**
31
     * @return array
32
     */
33 1
    public function getProjects() {
34 1
        return $this->em->getRepository(Project::class)->findAll();
35
    }
36
37
    /**
38
     * @param string $slug
39
     * @throws NotFoundHttpException
40
     * @return ProjectModel
41
     */
42 1
    public function getProject($slug) {
43 1
        $project = $this->em->getRepository(Project::class)->findOneBySlug($slug);
44 1
        if($project === null) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after IF keyword; 0 found
Loading history...
45
            throw new NotFoundHttpException('Project not found');
46
        }
47 1
        return $project;
48
    }
49
50
    /**
51
     * @param string $name
52
     * @param UserInterface $productOwner
53
     * @return \DevelopTech\AgilityBundle\ProjectModel
54
     */
55 1
    public function createProject($name, UserInterface $productOwner) {
56
        $project =
57 1
            (new Project())
58 1
            ->setName($name)
59 1
            ->setSlug($this->slugger->slugify($name))
60 1
            ->setProductOwner($productOwner)
61 1
            ->setBetaTestStatus('closed')
62 1
            ->setNbBetaTesters(0)
63 1
        ;
64 1
        $this->em->persist($project);
65 1
        $this->em->flush();
66 1
        return $project;
67
    }
68
69
    /**
70
     * @param integer $id
71
     * @param string $name
72
     * @param integer $betaTestStatus
73
     * @param integer $nbBetaTesters
74
     * @param UserInterface $productOwner
75
     * @throws NotFoundHttpException
76
     * @return ProjectModel
77
     */
78 2
    public function editProject($id, $name, $betaTestStatus, $nbBetaTesters, UserInterface $productOwner = null) {
79 2
        if (($project = $this->em->getRepository(Project::class)->find($id)) === null) {
80 1
            throw new NotFoundHttpException('Project not found');
81
        }
82
        $project
83 1
            ->setName($name)
84 1
            ->setSlug($this->slugger->slugify($name))
85 1
            ->setBetaTestStatus($betaTestStatus)
86 1
            ->setNbBetaTesters($nbBetaTesters)
87
        ;
88 1
        if($productOwner !== null) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after IF keyword; 0 found
Loading history...
89
            $project->setProductOwner($productOwner);
90
        }
91 1
        $this->em->flush();
92 1
        return $project;
93
    }
94
}
95