ChecksServer::checkServerAndAbort()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 4
nop 1
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Acacha\ForgePublish\Commands\Traits;
4
5
/**
6
 * Trait ChecksServer.
7
 *
8
 * @package Acacha\ForgePublish\Commands\Traits
9
 */
10
trait ChecksServer
11
{
12
    use ItFetchesServers;
13
14
    /**
15
     * Check server and abort.
16
     *
17
     * @param null $server
18
     */
19
    protected function checkServerAndAbort($server = null)
20
    {
21
        $server = $server ? $server : fp_env('ACACHA_FORGE_SERVER');
22
        if (! $this->checkServer($server)) {
23
            $this->error('Server ' . $server . ' not valid');
0 ignored issues
show
Bug introduced by
It seems like error() 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...
24
            die();
0 ignored issues
show
Coding Style Compatibility introduced by
The method checkServerAndAbort() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
25
        }
26
    }
27
28
    /**
29
     * Check value in servers.
30
     *
31
     * @param $env_var
32
     * @param $field
33
     * @param null $value
34
     * @param null $servers
35
     * @return bool
36
     */
37
    protected function checkValue($env_var, $field, $value = null, $servers = null)
38
    {
39
        $value = $value ? $value : fp_env($env_var);
40
        $servers = $servers ? $servers : $this->obtainServers();
41
        return in_array($value, collect($servers)->pluck($field)->toArray());
42
    }
43
44
    /**
45
     * Obtain servers.
46
     *
47
     * @return array|mixed
48
     */
49
    protected function obtainServers()
50
    {
51
        return isset($this->servers) ? $this->servers : $this->fetchServers();
0 ignored issues
show
Bug introduced by
The property servers 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...
52
    }
53
54
    /**
55
     * Check server id.
56
     *
57
     * @param null $server
58
     * @param null $servers
59
     * @return bool
60
     */
61
    protected function checkServer($server = null, $servers = null)
62
    {
63
        return $this->checkValue('ACACHA_FORGE_SERVER', 'forge_id', $server, $servers);
64
    }
65
66
    /**
67
     * Check ip.
68
     *
69
     * @param null $ip
70
     * @param null $servers
71
     * @return bool
72
     */
73
    protected function checkIp($ip = null, $servers = null)
74
    {
75
        return $this->checkValue('ACACHA_FORGE_IP_ADDRESS', 'ipAddress', $ip, $servers);
76
    }
77
78
    /**
79
     * Check server name.
80
     *
81
     * @param null $server_name
82
     * @param null $servers
83
     * @return bool
84
     */
85
    protected function checkServerName($server_name = null, $servers = null)
86
    {
87
        return $this->checkValue('ACACHA_FORGE_SERVER_NAME', 'name', $server_name, $servers);
88
    }
89
}
90