Completed
Push — feature/issue-40 ( 3d69a6...1b01be )
by Mikaël
24:23
created

Composer::encodeToJson()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.1481

Importance

Changes 2
Bugs 2 Features 0
Metric Value
c 2
b 2
f 0
dl 0
loc 9
ccs 4
cts 6
cp 0.6667
rs 9.6666
cc 2
eloc 6
nc 2
nop 1
crap 2.1481
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->addAutoloadToComposerJson();
41
42 24
        if ($this->getRunComposerUpdate() === true) {
43 20
            return $composer->run(new ArrayInput(array(
44 20
                'command' => 'update',
45 15
                '--verbose' => true,
46 15
                '--optimize-autoloader' => true,
47 15
                '--no-dev' => true,
48 20
                '--working-dir' => $this->getGenerator()->getOptionDestination(),
49 15
            )));
50
        }
51 4
        return 1;
52
    }
53
    /**
54
     * @return Composer
55
     */
56 24
    protected function addAutoloadToComposerJson()
57
    {
58 24
        $content = $this->getComposerFileContent();
59 24
        if (is_array($content) && !empty($content)) {
60 24
            $content['autoload'] = array(
61 24
                'psr-4' => $this->getPsr4Autoload(),
62
            );
63 18
        }
64 24
        return $this->setComposerFileContent($content);
65
    }
66
    /**
67
     * @return array
68
     */
69 24
    protected function getPsr4Autoload()
70
    {
71 24
        $namespace = new EmptyModel($this->getGenerator(), '');
72 24
        if ($namespace->getNamespace() !== '') {
73 4
            $namespaceKey = sprintf('%s\\', $namespace->getNamespace());
74 3
        } else {
75 20
            $namespaceKey = '';
76
        }
77
        return array(
78 24
            $namespaceKey => sprintf('./%s', AbstractModelFile::SRC_FOLDER),
79 18
        );
80
    }
81
    /**
82
     * @return array
83
     */
84 24
    protected function getComposerFileContent()
85 3
    {
86 24
        $content = array();
87 24
        $composerFilePath = $this->getComposerFilePath();
88 24
        if (!empty($composerFilePath)) {
89 24
            $content = json_decode(file_get_contents($composerFilePath), true);
90 18
        }
91 24
        return $content;
92
    }
93
    /**
94
     * @param array $content
95
     * @return Composer
96
     */
97 24
    protected function setComposerFileContent(array $content)
98
    {
99 24
        $composerFilePath = $this->getComposerFilePath();
100 24
        if (!empty($composerFilePath)) {
101 24
            file_put_contents($composerFilePath, self::encodeToJson($content));
102 18
        }
103 24
        return $this;
104
    }
105
    /**
106
     * @param array $content
107
     * @return string
108
     */
109 24
    protected static function encodeToJson($content)
110
    {
111 24
        if (version_compare(PHP_VERSION, '5.4.0') === -1) {
112
            $json = str_replace('\/', '/', json_encode($content));
113
        } else {
114 24
            $json = json_encode($content, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
115
        }
116 24
        return $json;
117
    }
118
    /**
119
     * @return string
120
     */
121 24
    protected function getComposerFilePath()
122
    {
123 24
        return realpath(sprintf('%s/composer.json', $this->getGenerator()->getOptionDestination()));
124
    }
125
    /**
126
     * @param bool $runComposerUpdate
127
     * @return Composer
128
     */
129 8
    public function setRunComposerUpdate($runComposerUpdate)
130
    {
131 8
        $this->runComposerUpdate = $runComposerUpdate;
132 8
        return $this;
133
    }
134
    /**
135
     * @return bool
136
     */
137 28
    public function getRunComposerUpdate()
138
    {
139 28
        return $this->runComposerUpdate;
140
    }
141
    /**
142
     * @return string
143
     */
144 8
    public function getFileExtension()
145
    {
146 8
        return self::JSON_FILE_EXTENSION;
147
    }
148
}
149