Completed
Push — master ( 1e7ae1...1641c1 )
by Axel
04:29
created

DetailsManager::getProjectDetails()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace App\Manager\Project;
4
5
use Doctrine\ORM\EntityManagerInterface;
6
7
use Symfony\Component\HttpFoundation\Session\SessionInterface;
8
9
use App\Entity\Project\Project;
10
use App\Entity\Project\Details;
11
12
class DetailsManager
13
{
14
    /** @var EntityManagerInterface **/
15
    protected $em;
16
    /** @var SessionInterface **/
17
    protected $session;
18
    
19 7
    public function __construct(EntityManagerInterface $em, SessionInterface $session)
20
    {
21 7
        $this->em = $em;
22 7
        $this->session = $session;
23 7
    }
24
    
25 7
    public function getCurrentProjectDetails(Project $project)
26
    {
27 7
        $results = $this->em->getRepository(Details::class)->findBy([
28 7
            'project' => $project
29
        ], [
30 7
            'updatedAt' => 'DESC'
31 7
        ], 1);
32 7
        return (count($results) > 0) ? $results[0] : null;
33
    }
34
    
35 3
    public function putProjectDetails(Project $project, array $data): Details
36
    {
37 3
        if (($details = $this->getCurrentProjectDetails($project)) === null) {
38 2
            $details = (new Details())->setProject($project);
39 2
            $this->em->persist($details);
40 2
            $this->session->getFlashbag()->add('success', 'projects.descriptions.first_completed');
41
        }
42
        $details
43 3
            ->setNeedDescription($data['need_description'])
44 3
            ->setTargetDescription($data['target_description'])
45 3
            ->setGoalDescription($data['goal_description'])
46
        ;
47 3
        $this->em->flush();
48 3
        return $details;
49
    }
50
}
51
52