Completed
Pull Request — master (#338)
by
unknown
09:23 queued 04:02
created

ModuleRepository::installNpm()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 29
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 4
eloc 13
c 2
b 0
f 0
nc 3
nop 1
dl 0
loc 29
rs 8.5806
1
<?php namespace Anomaly\Streams\Platform\Addon\Module;
2
3
use Anomaly\Streams\Platform\Addon\Module\Contract\ModuleRepositoryInterface;
4
use Symfony\Component\Process\Process;
5
6
/**
7
 * Class ModuleRepository
8
 *
9
 * @link   http://pyrocms.com/
10
 * @author PyroCMS, Inc. <[email protected]>
11
 * @author Ryan Thompson <[email protected]>
12
 */
13
class ModuleRepository implements ModuleRepositoryInterface
14
{
15
16
    /**
17
     * The module model.
18
     *
19
     * @var
20
     */
21
    protected $model;
22
23
    /**
24
     * Create a new ModuleRepository instance.
25
     *
26
     * @param ModuleModel $model
27
     */
28
    public function __construct(ModuleModel $model)
29
    {
30
        $this->model = $model;
31
    }
32
33
    /**
34
     * Return all modules in the database.
35
     *
36
     * @return mixed
37
     */
38
    public function all()
39
    {
40
        return $this->model->all();
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->model->all(); of type Illuminate\Database\Eloq...base\Eloquent\Builder[] adds the type Illuminate\Database\Eloquent\Builder[] to the return on line 40 which is incompatible with the return type declared by the interface Anomaly\Streams\Platform...epositoryInterface::all of type Anomaly\Streams\Platform\Model\EloquentCollection.
Loading history...
41
    }
42
43
    /**
44
     * Create a module record.
45
     *
46
     * @param  Module $module
47
     * @return bool
48
     */
49
    public function create(Module $module)
50
    {
51
        $instance = $this->model->newInstance();
52
53
        $instance->namespace = $module->getNamespace();
0 ignored issues
show
Documentation introduced by
The property namespace does not exist on object<Anomaly\Streams\P...don\Module\ModuleModel>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
54
        $instance->installed = false;
0 ignored issues
show
Documentation introduced by
The property installed does not exist on object<Anomaly\Streams\P...don\Module\ModuleModel>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
55
        $instance->enabled   = false;
0 ignored issues
show
Documentation introduced by
The property enabled does not exist on object<Anomaly\Streams\P...don\Module\ModuleModel>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
56
57
        return $instance->save();
58
    }
59
60
    /**
61
     * Delete a module record.
62
     *
63
     * @param  Module      $module
64
     * @return ModuleModel
65
     */
66
    public function delete(Module $module)
67
    {
68
        $module = $this->model->findByNamespace($module->getNamespace());
69
70
        if ($module) {
71
            $module->delete();
72
        }
73
74
        return $module;
75
    }
76
77
    /**
78
     * Mark a module as installed.
79
     *
80
     * @param  Module $module
81
     * @return bool
82
     */
83
    public function install(Module $module)
84
    {
85
        if (!$module = $this->model->findByNamespaceOrNew($module->getNamespace())) {
86
            return false;
87
        }
88
89
        $module->installed = true;
0 ignored issues
show
Documentation introduced by
The property installed does not exist on object<Anomaly\Streams\P...don\Module\ModuleModel>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
90
        $module->enabled   = true;
0 ignored issues
show
Documentation introduced by
The property enabled does not exist on object<Anomaly\Streams\P...don\Module\ModuleModel>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
91
92
        return $module->save();
93
    }
94
95
    /**
96
     * Mark a module as uninstalled.
97
     *
98
     * @param  Module $module
99
     * @return bool
100
     */
101
    public function uninstall(Module $module)
102
    {
103
        if (!$module = $this->model->findByNamespace($module->getNamespace())) {
104
            return false;
105
        }
106
107
        $module->installed = false;
0 ignored issues
show
Documentation introduced by
The property installed does not exist on object<Anomaly\Streams\P...don\Module\ModuleModel>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
108
        $module->enabled   = false;
0 ignored issues
show
Documentation introduced by
The property enabled does not exist on object<Anomaly\Streams\P...don\Module\ModuleModel>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
109
110
        return $module->save();
111
    }
112
113
    /**
114
     * Mark a module as disabled.
115
     *
116
     * @param  Module $module
117
     * @return bool
118
     */
119
    public function disable(Module $module)
120
    {
121
        $module = $this->model->findByNamespace($module->getNamespace());
122
123
        $module->enabled = false;
0 ignored issues
show
Documentation introduced by
The property enabled does not exist on object<Anomaly\Streams\P...don\Module\ModuleModel>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
124
125
        return $module->save();
126
    }
127
128
    /**
129
     * Mark a module as enabled.
130
     *
131
     * @param  Module $module
132
     * @return bool
133
     */
134
    public function enabled(Module $module)
135
    {
136
        $module = $this->model->findByNamespace($module->getNamespace());
137
138
        $module->enabled = true;
0 ignored issues
show
Documentation introduced by
The property enabled does not exist on object<Anomaly\Streams\P...don\Module\ModuleModel>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
139
140
        return $module->save();
141
    }
142
143
    /**
144
     * Install the npm dependencies
145
     * @return void
146
     */
147
    public function installNpm(Module $module)
148
    {
149
        // First make sure that there's a package.json
150
        if(file_exists($module->getAppPath() . '/package.json')) {
151
            return false;
152
        }
153
154
        $yarnCheck = new Process('which yarn');
155
        $yarnCheck->run();
156
157
        // Then check to see if yarn is installed and there's a yarn.lock file
158
        // Either way, add the folder as a dependency to the project's package.json
159
        if(file_exists(base_path().'/yarn.lock') && $yarnCheck->getOutput()) {
160
            $addProcess = new Process('yarn add file:' . $module->getAppPath() . ' --save && yarn install');
161
        } else {
162
            $addProcess = new Process('npm install ' . $module->getAppPath() . ' --save');
163
        }
164
165
        // Make sure the working directory is the project's root
166
        $addProcess->setWorkingDirectory(base_path());
167
168
        // Make sure it doesn't timeout
169
        // @todo: might need to increase the timeout
170
        $addProcess->setTimeout(7 * 60);
171
        $addProcess->setIdleTimeout(7 * 60);
172
173
        // Let 'er rip!
174
        $addProcess->run();
175
    }
176
177
}
178