CustomManagedCloudStorage::init()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
namespace STS\StorageConnect\Models;
4
5
use Carbon\Carbon;
6
7
/**
8
 * Class CustomManagedCloudStorage
9
 * @package STS\StorageConnect\Models
10
 */
11
class CustomManagedCloudStorage extends CloudStorage
12
{
13
    /**
14
     * @var callable
15
     */
16
    protected $saveCallback;
17
18
    /**
19
     * @param string $driver
20
     * @param callable $saveCallback
21
     *
22
     * @return CustomManagedCloudStorage
23
     */
24
    public static function init($driver, $saveCallback)
25
    {
26
        return (new static)
27
            ->setSaveCallback($saveCallback)
28
            ->fill([
29
                'driver' => $driver,
30
                'id'     => 0
31
            ]);
32
    }
33
34
    /**
35
     * @param array $attributes
36
     * @param callable $saveCallback
37
     *
38
     * @return $this
39
     */
40
    public function restore(array $attributes, $saveCallback)
41
    {
42
        $this->fill($attributes);
43
        $this->saveCallback = $saveCallback;
44
        $this->exists = true;
45
46
        return $this;
47
    }
48
49
    /**
50
     * @param array $options
51
     *
52
     * @return void
53
     */
54
    public function save(array $options = [])
55
    {
56
        if (!$this->created_at) {
0 ignored issues
show
Documentation introduced by
The property created_at does not exist on object<STS\StorageConnec...tomManagedCloudStorage>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read 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.");
        }
    }

}

If the property has read access only, you can use the @property-read 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...
57
            $this->setCreatedAt(Carbon::now());
58
        }
59
        $this->setUpdatedAt(Carbon::now());
60
61
        call_user_func_array($this->saveCallback, [$this->toJson(), $this->driver]);
0 ignored issues
show
Documentation introduced by
The property driver does not exist on object<STS\StorageConnec...tomManagedCloudStorage>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read 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.");
        }
    }

}

If the property has read access only, you can use the @property-read 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...
62
    }
63
64
    /**
65
     * @param $callback
66
     *
67
     * @return $this
68
     */
69
    public function setSaveCallback($callback)
70
    {
71
        $this->saveCallback = $callback;
72
73
        return $this;
74
    }
75
}