Passed
Branch 4.9 (cb955a)
by Mikhail
01:30
created

Addon::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 26
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 26
c 0
b 0
f 0
rs 9.7333
cc 1
nc 1
nop 3
1
<?php
2
3
namespace controllers;
4
5
use generators\AddonXml\AddonXmlGenerator;
6
use generators\Language\LanguageGenerator;
7
use generators\Readme\ReadmeGenerator;
8
use generators\MultipleFileGenerator;
9
use generators\FileGenerator;
10
use terminal\Terminal;
11
use filesystem\Filesystem;
12
use mediators\GeneratorMediator;
13
use \Config;
14
15
class Addon extends AbstractController
16
{
17
    private $config;
18
    private $terminal;
19
    private $filesystem;
20
    private $mfGenerator;
21
22
    use HelpTrait;
23
24
    function __construct(
25
        Config              $config,
26
        Terminal            $terminal,
27
        Filesystem          $filesystem
28
    )
29
    {
30
        $this->config               = $config;
31
        $this->terminal             = $terminal;
32
        $this->filesystem           = $filesystem;
33
34
        $addonXmlGenerator  = new AddonXmlGenerator($this->config);
35
        $languageGenerator  = new LanguageGenerator($this->config);
36
        $readmeGenerator    = new ReadmeGenerator($this->config);
37
38
        $generatorMediator  = new GeneratorMediator();
39
40
        $generatorMediator
41
            ->addGenerator($addonXmlGenerator)
42
            ->addGenerator($languageGenerator)
43
            ->addGenerator($readmeGenerator);
44
45
        $this->mfGenerator = new MultipleFileGenerator($this->filesystem);
46
        $this->mfGenerator
47
            ->addGenerator($addonXmlGenerator)
48
            ->addGenerator($languageGenerator)
49
            ->addGenerator($readmeGenerator);
50
    }
51
    
52
    /**
53
     * help:
54
     * addon create
55
     * creates addon.xml, language file, readme and other
56
     * @throws Exception if file already exists
57
     */
58
    public function create()
59
    {
60
        $this->mfGenerator
61
            ->throwIfExists('File already exists. Remove it first if you want to replace it.')
62
            ->find(AddonXmlGenerator::class)
63
                ->extract()
0 ignored issues
show
Bug introduced by
The method extract() does not exist on generators\AbstractFileGenerator. Since it exists in all sub-types, consider adding an abstract or default implementation to generators\AbstractFileGenerator. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

63
                ->/** @scrutinizer ignore-call */ extract()
Loading history...
64
                    ->create();
65
66
        $this->mfGenerator
67
            ->excluding(ReadmeGenerator::class)
68
                ->write()
69
                ->throwIfNotExists('File cannot be created.');
70
71
        $this->mfGenerator
72
            ->find(ReadmeGenerator::class)
73
                ->readFromTemplate()
74
                ->write();
75
76
        $self = $this;
77
        $this->mfGenerator->each(function($generator) use ($self) {
78
            $self->terminal->success($generator->extract()->getPath() . ' was created');
79
            $self->terminal->diff(
80
                \Diff::toString(\Diff::compare('', $generator->extract()->toString()))
81
            );
82
        });
83
    }
84
85
    /**
86
     * help:
87
     * addon remove
88
     * removes entire path of the addon
89
     */
90
    public function remove()
91
    {
92
        $addonPath = $this->config->get('path')
93
            . $this->config->get('filesystem.output_path_relative');
94
95
        $this->filesystem->delete($addonPath);
96
97
        if ($this->filesystem->exists($addonPath)) {
98
            throw new \Exception($addonPath . ' cannot be removed');
99
        }
100
    }
101
}
102