Passed
Push — master ( a600df...e0fe4b )
by FX
02:35
created

ProjectService   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 44
rs 10
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A setSlugAndToken() 0 16 2
A __construct() 0 4 1
1
<?php
2
3
/**
4
 * Copyright (c) 2017 Francois-Xavier Soubirou.
5
 *
6
 * This file is part of ci-report.
7
 *
8
 * ci-report is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU General Public License as published by
10
 * the Free Software Foundation, either version 3 of the License, or
11
 * (at your option) any later version.
12
 *
13
 * ci-report is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
 * GNU General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU General Public License
19
 * along with ci-report. If not, see <http://www.gnu.org/licenses/>.
20
 */
21
declare(strict_types=1);
22
23
namespace AppBundle\Service;
24
25
use AppBundle\Entity\Project;
26
use Doctrine\Common\Persistence\ManagerRegistry;
27
28
/**
29
 * Project service class.
30
 *
31
 * @category  ci-report app
32
 *
33
 * @author    Francois-Xavier Soubirou <[email protected]>
34
 * @copyright 2017 Francois-Xavier Soubirou
35
 * @license   http://www.gnu.org/licenses/   GPLv3
36
 *
37
 * @see      https://ci-report.io
38
 */
39
class ProjectService
40
{
41
    /**
42
     * @var ManagerRegistry
43
     */
44
    private $doctrine;
45
46
    /**
47
     * @var UtilService
48
     */
49
    private $utilService;
50
51
    /**
52
     * Constructor.
53
     *
54
     * @param ManagerRegistry $doctrine Doctrine registry manager
55
     */
56
    public function __construct(ManagerRegistry $doctrine)
57
    {
58
        $this->doctrine = $doctrine;
59
        $this->utilService = new UtilService();
60
    }
61
62
    /**
63
     * Generate a unique slug and token for a project.
64
     *
65
     * @param Project $project Project
66
     */
67
    public function setSlugAndToken(Project $project): void
68
    {
69
        $repository = $this->doctrine->getRepository(Project::class);
70
        $slug = $this->utilService->toAscii($project->getName());
71
72
        $root = $slug;
73
        $separator = '-';
74
        $ext = 0;
75
76
        while ($repository->refIdExists($slug)) {
77
            ++$ext;
78
            $slug = $root.$separator.$ext;
79
        }
80
        $project->setRefId($slug);
81
82
        $project->setToken($this->utilService->generateToken());
83
    }
84
}
85