Completed
Push — 1.x ( 1ecab6...509f36 )
by Mikaël
65:22 queued 63:08
created

Composer   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 158
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 19
c 0
b 0
f 0
lcom 1
cbo 5
dl 0
loc 158
ccs 77
cts 77
cp 1
rs 10

12 Methods

Rating   Name   Duplication   Size   Complexity  
B writeFile() 0 32 2
A completeComposerJson() 0 10 3
A addComposerSettings() 0 5 1
A getComposerFileContent() 0 9 2
A setComposerFileContent() 0 8 2
A getComposerFilePath() 0 4 1
A setRunComposerUpdate() 0 5 1
A getRunComposerUpdate() 0 4 1
A getFileExtension() 0 4 1
A encodeToJson() 0 9 2
A addAutoloadToComposerJson() 0 7 1
A getPsr4Autoload() 0 12 2
1
<?php
2
3
namespace WsdlToPhp\PackageGenerator\File;
4
5
use Composer\Console\Application;
6
use Symfony\Component\Console\Input\ArrayInput;
7
use WsdlToPhp\PackageGenerator\Model\EmptyModel;
8
9
class Composer extends AbstractFile
10
{
11
    const JSON_FILE_EXTENSION = 'json';
12
    /**
13
     * Tests purpose: do not run composer update command
14
     * @var bool
15
     */
16
    protected $runComposerUpdate = true;
17
    /**
18
     * @see \WsdlToPhp\PackageGenerator\File\AbstractFile::writeFile()
19
     * @return int|bool
20
     */
21 24
    protected function writeFile()
22
    {
23 24
        $composer = new Application();
24 24
        $composer->setAutoExit(false);
25
26 24
        $composer->run(new ArrayInput(array(
27 24
            'command' => 'init',
28 18
            '--verbose' => true,
29 18
            '--no-interaction' => true,
30 24
            '--name' => $this->getGenerator()->getOptionComposerName(),
31 24
            '--description' => sprintf('Package generated from %s using wsdltophp/packagegenerator', $this->getGenerator()->getWsdl()->getName()),
32
            '--require' => array(
33 18
                'php:>=5.3.3',
34 18
                'ext-soap:*',
35 18
                'wsdltophp/packagebase:dev-master',
36 18
            ),
37 24
            '--working-dir' => $this->getGenerator()->getOptionDestination(),
38 18
        )));
39
40 24
        $this->completeComposerJson();
41
42 24
        if ($this->getRunComposerUpdate() === true) {
43 16
            return $composer->run(new ArrayInput(array(
44 16
                'command' => 'update',
45 12
                '--verbose' => true,
46 12
                '--optimize-autoloader' => true,
47 12
                '--no-dev' => true,
48 16
                '--working-dir' => $this->getGenerator()->getOptionDestination(),
49 12
            )));
50
        }
51 8
        return 1;
52
    }
53
    /**
54
     * @return Composer
55
     */
56 24
    protected function completeComposerJson()
57
    {
58 24
        $content = $this->getComposerFileContent();
59 24
        if (is_array($content) && !empty($content)) {
60 18
            $this
61 24
                ->addAutoloadToComposerJson($content)
62 24
                ->addComposerSettings($content);
63 18
        }
64 24
        return $this->setComposerFileContent($content);
65
    }
66
    /**
67
     * @return Composer
68
     */
69 24
    protected function addAutoloadToComposerJson(array &$content)
70
    {
71 24
        $content['autoload'] = array(
72 24
            'psr-4' => $this->getPsr4Autoload(),
73 3
        );
74 24
        return $this;
75
    }
76
    /**
77
     * @return Composer
78
     */
79 24
    protected function addComposerSettings(array &$content)
80
    {
81 24
        $content = array_merge_recursive($content, $this->getGenerator()->getOptionComposerSettings());
82 24
        return $this;
83
    }
84
    /**
85
     * @return array
86
     */
87 24
    protected function getPsr4Autoload()
88
    {
89 24
        $namespace = new EmptyModel($this->getGenerator(), '');
90 24
        if ($namespace->getNamespace() !== '') {
91 8
            $namespaceKey = sprintf('%s\\', $namespace->getNamespace());
92 6
        } else {
93 16
            $namespaceKey = '';
94
        }
95
        return array(
96 24
            $namespaceKey => sprintf('./%s', AbstractModelFile::SRC_FOLDER),
97 18
        );
98
    }
99
    /**
100
     * @return array
101
     */
102 24
    protected function getComposerFileContent()
103
    {
104 24
        $content = array();
105 24
        $composerFilePath = $this->getComposerFilePath();
106 24
        if (!empty($composerFilePath)) {
107 24
            $content = json_decode(file_get_contents($composerFilePath), true);
108 18
        }
109 24
        return $content;
110
    }
111
    /**
112
     * @param array $content
113
     * @return Composer
114
     */
115 24
    protected function setComposerFileContent(array $content)
116
    {
117 24
        $composerFilePath = $this->getComposerFilePath();
118 24
        if (!empty($composerFilePath)) {
119 24
            file_put_contents($composerFilePath, self::encodeToJson($content));
120 18
        }
121 24
        return $this;
122
    }
123
    /**
124
     * @param array $content
125
     * @return string
126
     */
127 24
    protected static function encodeToJson($content)
128
    {
129 24
        if (version_compare(PHP_VERSION, '5.4.0') === -1) {
130 6
            $json = str_replace('\/', '/', json_encode($content));
131 6
        } else {
132 18
            $json = json_encode($content, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
133
        }
134 24
        return $json;
135
    }
136
    /**
137
     * @return string
138
     */
139 24
    protected function getComposerFilePath()
140
    {
141 24
        return realpath(sprintf('%s/composer.json', $this->getGenerator()->getOptionDestination()));
142
    }
143
    /**
144
     * @param bool $runComposerUpdate
145
     * @return Composer
146
     */
147 12
    public function setRunComposerUpdate($runComposerUpdate)
148
    {
149 12
        $this->runComposerUpdate = $runComposerUpdate;
150 12
        return $this;
151
    }
152
    /**
153
     * @return bool
154
     */
155 28
    public function getRunComposerUpdate()
156
    {
157 28
        return $this->runComposerUpdate;
158
    }
159
    /**
160
     * @return string
161
     */
162 12
    public function getFileExtension()
163
    {
164 12
        return self::JSON_FILE_EXTENSION;
165
    }
166
}
167