ProjectService::updateProject()   D
last analyzed

Complexity

Conditions 8
Paths 128

Size

Total Lines 43
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 24
CRAP Score 8.1867

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 43
ccs 24
cts 28
cp 0.8571
rs 4.6666
cc 8
eloc 21
nc 128
nop 5
crap 8.1867
1
<?php
2
/**
3
 * PHPCI - Continuous Integration for PHP.
4
 *
5
 * @copyright    Copyright 2014, Block 8 Limited.
6
 * @license      https://github.com/Block8/PHPCI/blob/master/LICENSE.md
7
 *
8
 * @link         https://www.phptesting.org/
9
 */
10
11
namespace PHPCI\Service;
12
13
use PHPCI\Model\Project;
14
use PHPCI\Store\ProjectStore;
15
16
/**
17
 * The project service handles the creation, modification and deletion of projects.
18
 * Class ProjectService.
19
 */
20
class ProjectService
21
{
22
    /**
23
     * @var \PHPCI\Store\ProjectStore
24
     */
25
    protected $projectStore;
26
27
    /**
28
     * @param ProjectStore $projectStore
29
     */
30 6
    public function __construct(ProjectStore $projectStore)
31
    {
32 6
        $this->projectStore = $projectStore;
33 6
    }
34
35
    /**
36
     * Create a new project model and use the project store to save it.
37
     *
38
     * @param string $title
39
     * @param string $type
40
     * @param string $reference
41
     * @param array  $options
42
     *
43
     * @return \PHPCI\Model\Project
44
     */
45 3
    public function createProject($title, $type, $reference, $options = array())
46
    {
47
        // Create base project and use updateProject() to set its properties:
48 3
        $project = new Project();
49
50 3
        return $this->updateProject($project, $title, $type, $reference, $options);
51
    }
52
53
    /**
54
     * Update the properties of a given project.
55
     *
56
     * @param Project $project
57
     * @param string  $title
58
     * @param string  $type
59
     * @param string  $reference
60
     * @param array   $options
61
     *
62
     * @return \PHPCI\Model\Project
63
     */
64 5
    public function updateProject(Project $project, $title, $type, $reference, $options = array())
65
    {
66
        // Set basic properties:
67 5
        $project->setTitle($title);
68 5
        $project->setType($type);
69 5
        $project->setReference($reference);
70 5
        $project->setAllowPublicStatus(0);
71
72
        // Handle extra project options:
73 5
        if (array_key_exists('ssh_private_key', $options)) {
74 2
            $project->setSshPrivateKey($options['ssh_private_key']);
75 2
        }
76
77 5
        if (array_key_exists('ssh_public_key', $options)) {
78 2
            $project->setSshPublicKey($options['ssh_public_key']);
79 2
        }
80
81 5
        if (array_key_exists('build_config', $options)) {
82 2
            $project->setBuildConfig($options['build_config']);
83 2
        }
84
85 5
        if (array_key_exists('allow_public_status', $options)) {
86 1
            $project->setAllowPublicStatus((int) $options['allow_public_status']);
87 1
        }
88
89 5
        if (array_key_exists('archived', $options)) {
90
            $project->setArchived((bool) $options['archived']);
91
        }
92
93 5
        if (array_key_exists('branch', $options)) {
94 1
            $project->setBranch($options['branch']);
95 1
        }
96
97 5
        if (array_key_exists('group', $options)) {
98
            $project->setGroup($options['group']);
99
        }
100
101
        // Allow certain project types to set access information:
102 5
        $this->processAccessInformation($project);
103
104
        // Save and return the project:
105 5
        return $this->projectStore->save($project);
106
    }
107
108
    /**
109
     * Delete a given project.
110
     *
111
     * @param Project $project
112
     *
113
     * @return bool
114
     */
115 1
    public function deleteProject(Project $project)
116
    {
117 1
        return $this->projectStore->delete($project);
118
    }
119
120
    /**
121
     * In circumstances where it is necessary, populate access information based on other project properties.
122
     *
123
     * @see ProjectService::createProject()
124
     *
125
     * @param Project $project
126
     */
127 5
    protected function processAccessInformation(Project &$project)
128
    {
129 5
        $matches = array();
130 5
        $reference = $project->getReference();
131
132 5
        if ($project->getType() == 'gitlab') {
133 1
            $info = array();
134
135 1
            if (preg_match('`^(.+)@(.+):([0-9]*)\/?(.+)\.git`', $reference, $matches)) {
136 1
                $info['user'] = $matches[1];
137 1
                $info['domain'] = $matches[2];
138 1
                $info['port'] = $matches[3];
139
140 1
                $project->setReference($matches[4]);
141 1
            }
142
143 1
            $project->setAccessInformation($info);
144 1
        }
145 5
    }
146
}
147