Completed
Pull Request — master (#2)
by Tim
02:51
created

Sync::sync()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 31
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 31
rs 8.439
c 1
b 0
f 0
cc 5
eloc 15
nc 5
nop 1
1
<?php
2
3
/**
4
 * AppserverIo\RoboTasks\Base\Sync
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\Base;
22
23
use Lurker\Resource\FileResource;
24
use Lurker\Event\FilesystemEvent;
25
use Robo\Common\BuilderAwareTrait;
26
use Robo\Contract\BuilderAwareInterface;
27
28
/**
29
 * A task that provides directory synchronization functionality.
30
 *
31
 * ```php
32
 * <?php
33
 * $this->taskSync()->src('src')->dest('dest')->run();
34
 * ?>
35
 * ```
36
 *
37
 * @author    Tim Wagner <[email protected]>
38
 * @copyright 2015 TechDivision GmbH <[email protected]>
39
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
40
 * @link      https://github.com/appserver-io/robo-tasks
41
 * @link      http://www.appserver.io
42
 */
43
class Sync extends Watch implements BuilderAwareInterface
44
{
45
46
    /**
47
     * The builder to internally create tasks.
48
     *
49
     * @var \Robo\Common\BuilderAwareTrait
50
     */
51
    use BuilderAwareTrait;
52
53
    /**
54
     * The source directory.
55
     *
56
     * @var array
57
     */
58
    protected $src;
59
60
    /**
61
     * The target directory.
62
     *
63
     * @var array
64
     */
65
    protected $dest;
66
67
    /**
68
     * Set the source directory.
69
     *
70
     * @param string $dest The source directory
0 ignored issues
show
Bug introduced by
There is no parameter named $dest. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
71
     *
72
     * @return \AppserverIo\RoboTasks\Base\Sync The task instance
73
     */
74
    public function src($src)
75
    {
76
        $this->src = $src;
77
        return $this;
78
    }
79
80
    /**
81
     * Set the destination directory.
82
     *
83
     * @param string $dest The destination directory
84
     *
85
     * @return \AppserverIo\RoboTasks\Base\Sync The task instance
86
     */
87
    public function dest($dest)
88
    {
89
        $this->dest = $dest;
0 ignored issues
show
Documentation Bug introduced by
It seems like $dest of type string is incompatible with the declared type array of property $dest.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
90
        return $this;
91
    }
92
93
    /**
94
     * Provides the main synchronize functionality.
95
     *
96
     * @param FilesystemEvent $event The event that has to be processed
97
     *
98
     * @return void
99
     * @throws \Exception Is thrown, if a non supported event type will be passed
100
     */
101
    public function sync(FilesystemEvent $event)
0 ignored issues
show
Coding Style Best Practice introduced by
Please use __construct() instead of a PHP4-style constructor that is named after the class.
Loading history...
102
    {
103
104
        // load the resource that changed
105
        $filename = $event->getResource();
106
107
        // prepare the destination filename
108
        $targetFilename = $this->prepareDestFilename($filename);
109
110
        // query whether or not it is a file
111
        if ($filename instanceof FileResource) {
112
            // query whether or not the file has to be copied or deleted
113
            switch ($event->getType()) {
114
                case $event->getType() === FilesystemEvent::DELETE:
115
                    // remove the target file
116
                    $this->collectionBuilder()->taskFilesystemStack()->remove($targetFilename)->run();
117
                    break;
118
119
                case $event->getType() === FilesystemEvent::CREATE:
120
                case $event->getType() === FilesystemEvent::MODIFY:
121
                    // if yes, copy it ot the target directory
122
                    $this->collectionBuilder()->taskFilesystemStack()->copy($filename, $targetFilename)->run();
123
                    break;
124
125
                default:
126
                    throw new \Exception(
127
                        sprintf('Found invalid event type %s', $event->getTypeString())
128
                    );
129
            }
130
        }
131
    }
132
133
    /**
134
     * Invokes the task and its functionality.
135
     *
136
     * @return \Robo\Result The tasks result
137
     * @see \Robo\Contract\TaskInterface::run()
138
     */
139
    public function run()
140
    {
141
142
        // add the monitor
143
        $this->monitor($this->src, array($this, 'sync'));
144
145
        // start synchronizing the directories
146
        return parent::run();
147
    }
148
149
    /**
150
     * Prepare and return the destination filename.
151
     *
152
     * @param string $filename The relative/absolute pathname of the file
153
     *
154
     * @return string The prepared destination filename
155
     */
156
    protected function prepareDestFilename($filename)
157
    {
158
        return sprintf(
159
            '%s%s',
160
            realpath($this->dest),
161
            str_replace(realpath($this->src), '', $filename)
162
        );
163
    }
164
}
165