Completed
Push — master ( f0b146...8f0e1e )
by Théo
02:31
created

AddPrefixCommandHandler::handle()   C

Complexity

Conditions 7
Paths 9

Size

Total Lines 37
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 7

Importance

Changes 0
Metric Value
dl 0
loc 37
ccs 21
cts 21
cp 1
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 24
nc 9
nop 3
crap 7
1
<?php
2
3
/*
4
 * This file is part of the webmozart/php-scoper package.
5
 *
6
 * (c) Bernhard Schussek <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Webmozart\PhpScoper\Handler;
13
14
use PhpParser\ParserFactory;
15
use Symfony\Component\Filesystem\Filesystem;
16
use Symfony\Component\Finder\Finder;
17
use Webmozart\PhpScoper\Exception\ParsingException;
18
use Webmozart\PhpScoper\Exception\RuntimeException;
19
use Webmozart\PhpScoper\Formatter\BasicFormatter;
20
use Webmozart\PhpScoper\Scoper;
21
22
/**
23
 * Handles the "add-prefix" command.
24
 *
25
 * @author Bernhard Schussek <[email protected]>
26
 */
27
class AddPrefixCommandHandler
28
{
29
    /**
30
     * @var Filesystem
31
     */
32
    private $filesystem;
33
34
    /**
35
     * @var Finder
36
     */
37
    private $finder;
38
39
    /**
40
     * @var Scoper
41
     */
42
    private $scoper;
43
44 6
    public function __construct()
45
    {
46 6
        $this->filesystem = new Filesystem();
47 6
        $this->finder = new Finder();
48
49 6
        $parser = (new ParserFactory())->create(ParserFactory::PREFER_PHP7);
50 6
        $this->scoper = new Scoper($parser);
51 6
    }
52
53
    /**
54
     * Handles the "add-prefix" command.
55
     *
56
     * @param string         $prefix
57
     * @param string[]       $paths
58
     * @param BasicFormatter $formatter
59
     *
60
     * @return int Returns 0 on success and a positive integer on error.
61
     */
62 6
    public function handle($prefix, array $paths, BasicFormatter $formatter)
63
    {
64 6
        $prefix = rtrim($prefix, '\\');
65 6
        $pathsToSearch = [];
66 6
        $filesToAppend = [];
67
68 6
        foreach ($paths as $path) {
69 6
            if (!$this->filesystem->isAbsolutePath($path)) {
70 6
                $path = getcwd().DIRECTORY_SEPARATOR.$path;
71
            }
72
73 6
            if (($exists = !file_exists($path)) || !is_readable($path)) {
74 1
                $issue = $exists ? 'does not exist' : 'is not readable';
75 1
                throw new RuntimeException(sprintf(
76 1
                    'A given path %s: %s',
77
                    $issue,
78
                    $path
79
                ));
80
            }
81
82 5
            if (is_dir($path)) {
83 2
                $pathsToSearch[] = $path;
84
            } else {
85 5
                $filesToAppend[] = $path;
86
            }
87
        }
88
89 5
        $this->finder->files()
90 5
            ->name('*.php')
91 5
            ->in($pathsToSearch)
92 5
            ->append($filesToAppend)
93 5
            ->sortByName();
94
95 5
        $this->scopeFiles($prefix, $formatter);
96
97 5
        return 0;
98
    }
99
100
    /**
101
     * Scopes all files attached to Finder instance.
102
     *
103
     * @param string         $prefix
104
     * @param BasicFormatter $formatter
105
     */
106 5
    private function scopeFiles($prefix, BasicFormatter $formatter)
107
    {
108 5
        $count = count($this->finder);
109 5
        $formatter->outputFileCount($count);
110
111 5
        foreach ($this->finder as $file) {
112 4
            $this->scopeFile($file->getPathName(), $prefix, $formatter);
113
        }
114 5
    }
115
116
    /**
117
     * Scopes a given file.
118
     *
119
     * @param string         $path
120
     * @param string         $prefix
121
     * @param BasicFormatter $formatter
122
     */
123 4
    private function scopeFile($path, $prefix, BasicFormatter $formatter)
124
    {
125 4
        $fileContent = file_get_contents($path);
126
        try {
127 4
            $scoppedContent = $this->scoper->addNamespacePrefix($fileContent, $prefix);
128 3
            $this->filesystem->dumpFile($path, $scoppedContent);
129 3
            $formatter->outputSuccess($path);
130 1
        } catch (ParsingException $exception) {
131 1
            $formatter->outputFail($path);
132
        }
133 4
    }
134
}
135