Completed
Pull Request — master (#2)
by Tim
08:14
created

AbstractRoboFile::getReportsDir()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
/**
4
 * AppserverIo\RoboTasks\AbstractRoboFile
5
 *
6
 * NOTICE OF LICENSE
7
 *
8
 * This source file is subject to the Open Software License (OSL 3.0)
9
 * that is available through the world-wide-web at this URL:
10
 * http://opensource.org/licenses/osl-3.0.php
11
 *
12
 * PHP version 5
13
 *
14
 * @author    Tim Wagner <[email protected]>
15
 * @copyright 2015 TechDivision GmbH <[email protected]>
16
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
17
 * @link      https://github.com/appserver-io/robo-tasks
18
 * @link      http://www.appserver.io
19
 */
20
21
namespace AppserverIo\RoboTasks;
22
23
use Robo\Robo;
24
use Robo\Tasks;
25
26
/**
27
 * Abstract implementation of a Robo configuration class.
28
 *
29
 * @author    Tim Wagner <[email protected]>
30
 * @copyright 2015 TechDivision GmbH <[email protected]>
31
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
32
 * @link      https://github.com/appserver-io/robo-tasks
33
 * @link      http://www.appserver.io
34
 */
35
abstract class AbstractRoboFile extends Tasks
36
{
37
38
    /**
39
     * Load the appserver.io base tasks.
40
     *
41
     * @var \AppserverIo\RoboTasks\Base\loadTasks
42
     */
43
    use Base\loadTasks;
44
45
    /**
46
     * Initialize the
47
     */
48
    public function __construct()
49
    {
50
51
        // load the configuration
52
        $config = Robo::config();
53
54
        // set the default values
55
        $config->setDefault(ConfigurationKeys::BASE_DIR, $baseDir = getcwd());
56
        $config->setDefault(ConfigurationKeys::SRC_DIR, $baseDir . DIRECTORY_SEPARATOR . 'src');
57
        $config->setDefault(ConfigurationKeys::DIST_DIR, $baseDir . DIRECTORY_SEPARATOR . 'dist');
58
        $config->setDefault(ConfigurationKeys::VENDOR_DIR, $baseDir . DIRECTORY_SEPARATOR . 'vendor');
59
        $config->setDefault(ConfigurationKeys::TARGET_DIR, $targetDir = $baseDir . DIRECTORY_SEPARATOR . 'target');
60
        $config->setDefault(ConfigurationKeys::REPORTS_DIR, $targetDir . DIRECTORY_SEPARATOR . 'reports');
61
    }
62
63
    /**
64
     * Sync's the extension with the Magento 2 sources.
65
     *
66
     * @param array $opts Array with commandline options
67
     *
68
     * @return void
69
     */
70
    public function sync(
71
        $opts = [
72
            InputOptionKeys::SRC_DIR => null,
73
            InputOptionKeys::TARGET_DIR => null
74
        ]
75
    ) {
76
77
        // initialize src/target directory
78
        $srcDir = $this->getSrcDir();
79
        $targetDir = $this->getTargetDir();
80
81
        // query whether or not the default source directory has been overwritten
82
        if ($opts[InputOptionKeys::SRC_DIR]) {
83
            $srcDir = $opts[InputOptionKeys::SRC_DIR];
84
        }
85
86
        // query whether or not the default target directory has been overwritten
87
        if ($opts[InputOptionKeys::TARGET_DIR]) {
88
            $targetDir = $opts[InputOptionKeys::TARGET_DIR];
89
        }
90
91
        // start watching the src directory
92
        $this->taskWatch()->monitor($srcDir, function(FilesystemEvent $event) use ($srcDir, $targetDir) {
93
             // load the resource that changed
94
             $filename = $event->getResource();
95
96
             // prepare the target filename
97
             $targetFilename = $this->prepareTargetFilename($srcDir, $targetDir, $filename);
98
99
             // query whether or not it is a file
100
             if ($filename instanceof FileResource) {
0 ignored issues
show
Bug introduced by
The class AppserverIo\RoboTasks\FileResource does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
101
                 // query whether or not the file has to be copied or deleted
102
                 switch ($event->getType()) {
103
                     case $event->getType() === FilesystemEvent::DELETE:
104
                         // remove the target file
105
                         $this->_remove($targetFilename);
106
                         break;
107
108
                     case $event->getType() === FilesystemEvent::CREATE:
109
                     case $event->getType() === FilesystemEvent::MODIFY:
110
                         // if yes, copy it ot the target directory
111
                         $this->taskFilesystemStack()
112
                              ->copy($filename, $targetFilename)
113
                              ->run();
114
                         break;
115
116
                     default:
117
                         throw new \Exception(
118
                             sprintf('Found invalid event type %s', $event->getTypeString())
119
                         );
120
                 }
121
             }
122
         })->run();
123
    }
124
125
    /**
126
     * Prepare and return the target filename.
127
     *
128
     * @param string $srcDir    The source filename
129
     * @param string $targetDir The relative/absolute target directory
130
     * @param string $filename  The relative/absolute pathname of the file
131
     *
132
     * @return string The prepared target filename
133
     */
134
    protected function prepareTargetFilename($srcDir, $targetDir, $filename)
135
    {
136
        return sprintf(
137
            '%s%s',
138
            realpath($targetDir),
139
            str_replace(realpath($srcDir), '', $filename)
140
        );
141
    }
142
143
    /**
144
     * Return the base directory.
145
     *
146
     * @return string The base directory
147
     */
148
    protected function getBaseDir()
149
    {
150
        return $this->get(ConfigurationKeys::BASE_DIR);
151
    }
152
153
    /**
154
     * Return the source directory.
155
     *
156
     * @return string The source directory
157
     */
158
    protected function getSrcDir()
159
    {
160
        return $this->get(ConfigurationKeys::SRC_DIR);
161
    }
162
163
    /**
164
     * Return the distribution directory.
165
     *
166
     * @return string The distribution directory
167
     */
168
    protected function getDistDir()
169
    {
170
        return $this->get(ConfigurationKeys::DIST_DIR);
171
    }
172
173
    /**
174
     * Return the target directory.
175
     *
176
     * @return string The target directory
177
     */
178
    protected function getTargetDir()
179
    {
180
        return $this->get(ConfigurationKeys::TARGET_DIR);
181
    }
182
183
    /**
184
     * Return the vendor directory.
185
     *
186
     * @return string The vendor directory
187
     */
188
    protected function getVendorDir()
189
    {
190
        return $this->get(ConfigurationKeys::VENDOR_DIR);
191
    }
192
193
    /**
194
     * Return the reports directory.
195
     *
196
     * @return string The reports directory
197
     */
198
    protected function getReportsDir()
199
    {
200
        return $this->get(ConfigurationKeys::REPORTS_DIR);
201
    }
202
203
    /**
204
     * Fetch a configuration value.
205
     *
206
     * @param string      $key             Which config item to look up
207
     * @param string|null $defaultOverride Override usual default value with a different default
208
     *
209
     * @return mixed
210
     */
211
    protected function get($key, $defaultOverride = null)
212
    {
213
        return Robo::config()->get($key, $defaultOverride);
214
    }
215
}
216