SourceWatcher   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 149
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 55
c 1
b 0
f 0
dl 0
loc 149
rs 10
wmc 16

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getStepLoader() 0 3 1
A getPipeline() 0 3 1
A load() 0 19 2
A extract() 0 19 2
A run() 0 3 1
A transform() 0 18 2
A flush() 0 3 1
A save() 0 35 5
1
<?php
2
3
namespace Coco\SourceWatcher\Core;
4
5
use Coco\SourceWatcher\Core\IO\Inputs\Input;
6
use Coco\SourceWatcher\Core\IO\Outputs\Output;
7
use Coco\SourceWatcher\Utils\Internationalization;
8
use Ramsey\Uuid\Uuid;
9
10
11
/**
12
 * Class SourceWatcher
13
 *
14
 * @package Coco\SourceWatcher\Core
15
 */
16
class SourceWatcher
17
{
18
    private StepLoader $stepLoader;
19
20
    private Pipeline $pipeline;
21
22
    public function __construct ()
23
    {
24
        $this->stepLoader = new StepLoader();
25
        $this->pipeline = new Pipeline();
26
    }
27
28
    public function getStepLoader () : StepLoader
29
    {
30
        return $this->stepLoader;
31
    }
32
33
    public function getPipeline () : Pipeline
34
    {
35
        return $this->pipeline;
36
    }
37
38
    /**
39
     * @param string $extractorName
40
     * @param Input|null $input
41
     * @param array $options
42
     * @return $this
43
     * @throws SourceWatcherException
44
     */
45
    public function extract ( string $extractorName, ?Input $input, array $options = [] ) : SourceWatcher
46
    {
47
        $extractor = $this->stepLoader->getStep( Extractor::class, $extractorName );
48
49
        if ( $extractor == null ) {
50
            throw new SourceWatcherException(
51
                sprintf(
52
                    Internationalization::getInstance()->getText( SourceWatcher::class, "Extractor_Not_Found" ),
53
                    $extractorName
54
                )
55
            );
56
        }
57
58
        $extractor->setInput( $input );
0 ignored issues
show
Bug introduced by
The method setInput() does not exist on Coco\SourceWatcher\Core\Step. It seems like you code against a sub-type of Coco\SourceWatcher\Core\Step such as Coco\SourceWatcher\Core\Extractor. ( Ignorable by Annotation )

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

58
        $extractor->/** @scrutinizer ignore-call */ 
59
                    setInput( $input );
Loading history...
59
        $extractor->options( $options );
60
61
        $this->pipeline->pipe( $extractor );
62
63
        return $this;
64
    }
65
66
    /**
67
     * @param string $transformerName
68
     * @param array $options
69
     * @return $this
70
     * @throws SourceWatcherException
71
     */
72
    public function transform ( string $transformerName, array $options = [] ) : SourceWatcher
73
    {
74
        $transformer = $this->stepLoader->getStep( Transformer::class, $transformerName );
75
76
        if ( $transformer == null ) {
77
            throw new SourceWatcherException(
78
                sprintf(
79
                    Internationalization::getInstance()->getText( SourceWatcher::class, "Transformer_Not_Found" ),
80
                    $transformerName
81
                )
82
            );
83
        }
84
85
        $transformer->options( $options );
86
87
        $this->pipeline->pipe( $transformer );
88
89
        return $this;
90
    }
91
92
    /**
93
     * @param string $loaderName
94
     * @param Output $output
95
     * @param array $options
96
     * @return $this
97
     * @throws SourceWatcherException
98
     */
99
    public function load ( string $loaderName, Output $output, array $options = [] ) : SourceWatcher
100
    {
101
        $loader = $this->stepLoader->getStep( Loader::class, $loaderName );
102
103
        if ( $loader == null ) {
104
            throw new SourceWatcherException(
105
                sprintf(
106
                    Internationalization::getInstance()->getText( SourceWatcher::class, "Loader_Not_Found" ),
107
                    $loaderName
108
                )
109
            );
110
        }
111
112
        $loader->setOutput( $output );
0 ignored issues
show
Bug introduced by
The method setOutput() does not exist on Coco\SourceWatcher\Core\Step. It seems like you code against a sub-type of Coco\SourceWatcher\Core\Step such as Coco\SourceWatcher\Core\Loader. ( Ignorable by Annotation )

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

112
        $loader->/** @scrutinizer ignore-call */ 
113
                 setOutput( $output );
Loading history...
113
        $loader->options( $options );
114
115
        $this->pipeline->pipe( $loader );
116
117
        return $this;
118
    }
119
120
    public function run () : void
121
    {
122
        $this->pipeline->execute();
123
    }
124
125
    public function flush () : void
126
    {
127
        $this->pipeline = new Pipeline();
128
    }
129
130
    public function save ( string $name = null ) : string
131
    {
132
        $arrayRepresentation = [];
133
134
        $steps = $this->pipeline->getSteps();
135
136
        foreach ( $steps as $currentStep ) {
137
            $arrayRepresentation[] = $currentStep->getArrayRepresentation();
138
        }
139
140
        $jsonRepresentation = json_encode( $arrayRepresentation, JSON_PRETTY_PRINT );
141
142
        $mainDirectory = $_SERVER["HOME"] . DIRECTORY_SEPARATOR . ".source-watcher";
143
144
        if ( !file_exists( $mainDirectory ) ) {
145
            mkdir( $mainDirectory, 0777, true );
146
        }
147
148
        $transformationsDirectory = $mainDirectory . DIRECTORY_SEPARATOR . "transformations";
149
150
        if ( !file_exists( $transformationsDirectory ) ) {
151
            mkdir( $transformationsDirectory, 0777, true );
152
        }
153
154
        if ( empty( $name ) ) {
155
            $uuid = Uuid::uuid4();
156
157
            $name = $uuid->toString();
158
        }
159
160
        $transformationFile = $transformationsDirectory . DIRECTORY_SEPARATOR . $name . ".swt";
161
162
        file_put_contents( $transformationFile, $jsonRepresentation );
163
164
        return $transformationFile;
165
    }
166
}
167