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

ProjectManager::getProjects()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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