Initializer   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 118
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 97.3%

Importance

Changes 0
Metric Value
dl 0
loc 118
ccs 36
cts 37
cp 0.973
rs 10
c 0
b 0
f 0
wmc 11
lcom 1
cbo 2

5 Methods

Rating   Name   Duplication   Size   Complexity  
A addTemplate() 0 6 1
A getTemplateNames() 0 4 1
A initialize() 0 15 2
A checkDirectoryBeforeInitialize() 0 29 5
A checkFileBeforeInitialize() 0 13 2
1
<?php
2
/* (c) Anton Medvedev <[email protected]>
3
 *
4
 * For the full copyright and license information, please view the LICENSE
5
 * file that was distributed with this source code.
6
 */
7
8
namespace Deployer\Initializer;
9
10
use Deployer\Initializer\Exception\IOException;
11
use Deployer\Initializer\Exception\TemplateNotFoundException;
12
use Deployer\Initializer\Template\TemplateInterface;
13
14
/**
15
 * Initializer system
16
 *
17
 * @author Vitaliy Zhuk <[email protected]>
18
 */
19
class Initializer
20
{
21
    /**
22
     * @var array|TemplateInterface[]
23
     */
24
    private $templates;
25
26
    /**
27
     * Add template to initializer
28
     *
29
     * @param string            $name
30
     * @param TemplateInterface $template
31
     *
32
     * @return Initializer
33
     */
34 23
    public function addTemplate($name, TemplateInterface $template)
35
    {
36 23
        $this->templates[$name] = $template;
37
38 23
        return $this;
39
    }
40
41
    /**
42
     * Get template names
43
     *
44
     * @return array
45
     */
46 17
    public function getTemplateNames()
47
    {
48 17
        return array_keys($this->templates);
49
    }
50
51
    /**
52
     * Initialize deployer in project
53
     *
54
     * @param string $template
55
     * @param string $directory
56
     * @param string $file
57
     * @param array $params
58
     * @return string The configuration file path
59
     *
60
     * @throws TemplateNotFoundException
61
     */
62 6
    public function initialize($template, $directory, $file = 'deploy.php', $params = [])
63
    {
64 6
        if (!isset($this->templates[$template])) {
65 1
            throw TemplateNotFoundException::create($template, array_keys($this->templates));
66
        }
67
68 5
        $this->checkDirectoryBeforeInitialize($directory);
69 2
        $this->checkFileBeforeInitialize($directory, $file);
70
71 1
        $filePath = $directory . '/' . $file;
72
73 1
        $this->templates[$template]->initialize($filePath, $params);
74
75 1
        return $filePath;
76
    }
77
78
    /**
79
     * Check the directory before initialize
80
     *
81
     * @param string $directory
82
     *
83
     * @throws IOException
84
     */
85 5
    private function checkDirectoryBeforeInitialize($directory)
86
    {
87 5
        if (!file_exists($directory)) {
88 1
            set_error_handler(function ($errCode, $errStr) use ($directory) {
89 1
                $parts = explode(':', $errStr, 2);
90 1
                $errorMessage = isset($parts[1]) ? trim($parts[1]) : 'Undefined';
91
92 1
                throw new IOException(sprintf(
93 1
                    'Could not create directory "%s". %s',
94
                    $directory,
95
                    $errorMessage
96
                ), $errCode);
97 1
            });
98
99 1
            mkdir($directory, 0775);
100
101
            restore_error_handler();
102 4
        } elseif (!is_dir($directory)) {
103 1
            throw new IOException(sprintf(
104 1
                'Can not create directory. The path "%s" already exist.',
105
                $directory
106
            ));
107 3
        } elseif (!is_writable($directory)) {
108 1
            throw new IOException(sprintf(
109 1
                'The directory "%s" is not writable.',
110
                $directory
111
            ));
112
        }
113 2
    }
114
115
    /**
116
     * Check the file before initialize
117
     *
118
     * @param string $directory
119
     * @param string $file
120
     *
121
     * @throws IOException
122
     */
123 2
    private function checkFileBeforeInitialize($directory, $file)
124
    {
125 2
        $filePath = $directory . '/' . $file;
126
127 2
        if (file_exists($filePath)) {
128 1
            throw new IOException(sprintf(
129 1
                'The file "%s" already exist.',
130
                $filePath
131
            ));
132
        }
133
134 1
        touch($filePath);
135 1
    }
136
}
137