Completed
Push — master ( a7122d...c2f186 )
by Tim
31s queued 11s
created

Sync   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 122
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 9
lcom 1
cbo 3
dl 0
loc 122
rs 10
c 2
b 0
f 0
ccs 0
cts 43
cp 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A src() 0 5 1
A dest() 0 5 1
B sync() 0 31 5
A run() 0 9 1
A prepareDestFilename() 0 8 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 $src The source directory
71
     *
72
     * @return \AppserverIo\RoboTasks\Base\Sync The task instance
73
     */
74
    public function src($src)
75
    {
76
        $this->src = $src;
0 ignored issues
show
Documentation Bug introduced by
It seems like $src of type string is incompatible with the declared type array of property $src.

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...
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