Passed
Push — master ( b96358...1b0bff )
by Mike
03:00
created

BuildModel::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 4
dl 0
loc 10
ccs 5
cts 5
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
4
namespace Xervice\Database\Business\Model;
5
6
7
use Symfony\Component\Finder\Finder;
8
use Xervice\Database\Provider\PropelCommandProviderInterface;
9
10
class BuildModel implements BuildModelInterface
11
{
12
    /**
13
     * @var \Xervice\Database\Provider\PropelCommandProviderInterface
14
     */
15
    private $propelCommandProvider;
16
17
    /**
18
     * @var array
19
     */
20
    private $schemaPaths;
21
22
    /**
23
     * @var string
24
     */
25
    private $schemaTarget;
26
27
    /**
28
     * @var string
29
     */
30
    private $schemaPattern;
31
32
    /**
33
     * BuildModel constructor.
34
     *
35
     * @param \Xervice\Database\Provider\PropelCommandProviderInterface $propelCommandProvider
36
     * @param array $schemaPaths
37
     */
38 1
    public function __construct(
39
        PropelCommandProviderInterface $propelCommandProvider,
40
        array $schemaPaths,
41
        string $schemaTarget,
42
        string $schemaPattern
43
    ) {
44 1
        $this->propelCommandProvider = $propelCommandProvider;
45 1
        $this->schemaPaths = $schemaPaths;
46 1
        $this->schemaTarget = $schemaTarget;
47 1
        $this->schemaPattern = $schemaPattern;
48 1
    }
49
50
    /**
51
     * @return array
52
     */
53 1
    public function buildModel(): array
54
    {
55 1
        if (!is_dir($this->schemaTarget)) {
56 1
            if (!mkdir($this->schemaTarget, 0777) && !is_dir($this->schemaTarget)) {
57
                throw new \RuntimeException(sprintf('Directory "%s" was not created', $this->schemaTarget));
58
            }
59
        }
60
61 1
        foreach ($this->schemaPaths as $schemaPath) {
62 1
            $finder = new Finder();
63 1
            $finder->files()->name('*' . $this->schemaPattern)->in($schemaPath);
64 1
            foreach ($finder as $schemaFile) {
65 1
                $targetFilename = str_replace($this->schemaPattern, '.schema.xml', basename($schemaFile->getRealPath()));
66 1
                copy($schemaFile->getRealPath(), $this->schemaTarget . '/' . $targetFilename);
67
            }
68
        }
69
70 1
        return $this->propelCommandProvider->execute('model:build');
71
    }
72
}