Completed
Push — master ( 2c4f4a...51d604 )
by Joseph
01:42
created

ManagesStorageConnection::ping()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
c 0
b 0
f 0
rs 9.9
cc 3
nc 3
nop 0
1
<?php
2
namespace STS\StorageConnect\Models\Concerns;
3
4
use STS\StorageConnect\Events\CloudStorageDisabled;
5
use STS\StorageConnect\Events\CloudStorageEnabled;
6
use STS\StorageConnect\Exceptions\StorageUnavailableException;
7
8
trait ManagesStorageConnection
9
{
10
    /**
11
     * @return bool
12
     */
13
    public function isReady()
14
    {
15
        if ($this->isFull()) {
0 ignored issues
show
Bug introduced by
It seems like isFull() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
16
            $this->ping();
17
        }
18
19
        return $this->isEnabled();
20
    }
21
22
    /**
23
     * Sometimes we want to gracefully check up on the cloud storage account without any exceptions
24
     */
25
    public function ping()
26
    {
27
        if (!$this->shouldCheckSpace()) {
0 ignored issues
show
Bug introduced by
It seems like shouldCheckSpace() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
28
            return;
29
        }
30
31
        try {
32
            $this->checkSpaceUsage();
0 ignored issues
show
Bug introduced by
It seems like checkSpaceUsage() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
33
        } catch (\Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
34
        }
35
    }
36
37
    /**
38
     * @return bool
39
     * @throws StorageUnavailableException
40
     */
41
    public function verify()
42
    {
43
        if (!$this->isReady()) {
44
            throw new StorageUnavailableException($this);
45
        }
46
47
        return true;
48
    }
49
50
    /**
51
     * @return mixed
52
     */
53
    public function isConnected()
54
    {
55
        return $this->connected;
0 ignored issues
show
Bug introduced by
The property connected does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
56
    }
57
58
    /**
59
     * @return bool
60
     */
61
    public function isEnabled()
62
    {
63
        return $this->isConnected() && $this->enabled;
0 ignored issues
show
Bug introduced by
The property enabled does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
64
    }
65
66
    /**
67
     * @return bool
68
     */
69
    public function isDisabled()
70
    {
71
        return !$this->isEnabled();
72
    }
73
74
    /**
75
     * @return bool
76
     */
77
    public function isTokenInvalid()
78
    {
79
        return $this->isDisabled() && $this->reason == self::INVALID_TOKEN;
0 ignored issues
show
Bug introduced by
The property reason does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
80
    }
81
82
    /**
83
     * @param null $reason
84
     *
85
     * @return $this
86
     */
87 View Code Duplication
    public function disable($reason = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
88
    {
89
        $this->enabled = 0;
90
        $this->reason = $reason;
91
92
        if ($reason == self::SPACE_FULL) {
93
            $this->full = 1;
0 ignored issues
show
Bug introduced by
The property full does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
94
        }
95
96
        $this->save();
0 ignored issues
show
Bug introduced by
It seems like save() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
97
        event(new CloudStorageDisabled($this));
98
99
        return $this;
100
    }
101
102
    /**
103
     * @return $this
104
     */
105 View Code Duplication
    public function enable()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
106
    {
107
        $this->reason = null;
108
        $this->enabled = 1;
109
        $this->full = 0;
110
111
        $this->save();
0 ignored issues
show
Bug introduced by
It seems like save() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
112
        event(new CloudStorageEnabled($this));
113
114
        return $this;
115
    }
116
}