Completed
Push — master ( e97c13...1021f1 )
by Timur
02:50
created

SiteResourceCommand::for()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
1
<?php
2
3
namespace Laravel\Forge\Sites\Commands;
4
5
use Laravel\Forge\Server;
6
use Laravel\Forge\Sites\Site;
7
use Psr\Http\Message\ResponseInterface;
8
9
abstract class SiteResourceCommand extends SiteCommand
10
{
11
    /**
12
     * Associated site.
13
     *
14
     * @var \Laravel\Forge\Sites\Site
15
     */
16
    protected $site;
17
18
    /**
19
     * Resource ID.
20
     *
21
     * @var int
22
     */
23
    protected $siteResourceId = 0;
24
25
    /**
26
     * Site resource path.
27
     *
28
     * @return string
29
     */
30
    abstract public function siteResourcePath();
31
32
    /**
33
     * Site resource class.
34
     *
35
     * @return string|null
36
     */
37
    public function siteResourceClass()
38
    {
39
        //
40
    }
41
42
    /**
43
     * Set associated site and execute command on site server.
44
     *
45
     * @param \Laravel\Forge\Sites\Site $site
46
     *
47
     * @return static
48
     */
49
    public function for(Site $site)
0 ignored issues
show
Coding Style introduced by
Possible parse error: non-abstract method defined as abstract
Loading history...
Coding Style introduced by
It is generally advisable to only define one property per statement.

Only declaring a single property per statement allows you to later on add doc comments more easily.

It is also recommended by PSR2, so it is a common style that many people expect.

Loading history...
50
    {
51
        $this->site = $site;
52
        $this->setItemId($site->id());
0 ignored issues
show
Coding Style introduced by
It is generally advisable to only define one property per statement.

Only declaring a single property per statement allows you to later on add doc comments more easily.

It is also recommended by PSR2, so it is a common style that many people expect.

Loading history...
Coding Style introduced by
The visibility should be declared for property $this.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
53
54
        return $this->on($site->getServer());
0 ignored issues
show
Coding Style introduced by
It is generally advisable to only define one property per statement.

Only declaring a single property per statement allows you to later on add doc comments more easily.

It is also recommended by PSR2, so it is a common style that many people expect.

Loading history...
Coding Style introduced by
The visibility should be declared for property $this.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
55
    }
56
57
    /**
58
     * Set site resource ID.
59
     *
60
     * @param int $resourceId
61
     *
62
     * @return static
63
     */
64
    public function setSiteResourceId(int $resourceId)
65
    {
66
        $this->siteResourceId = $resourceId;
67
68
        return $this;
69
    }
70
71
    /**
72
     * Get site resource ID.
73
     *
74
     * @return int
75
     */
76
    public function getSiteResourceId(): int
77
    {
78
        return $this->siteResourceId;
79
    }
80
81
    /**
82
     * Get associated site.
83
     *
84
     * @return \Laravel\Forge\Sites\Site
85
     */
86
    public function getSite(): Site
87
    {
88
        return $this->site;
89
    }
90
91
    /**
92
     * HTTP request URL.
93
     *
94
     * @param \Laravel\Forge\Server
95
     *
96
     * @return string
97
     */
98
    public function requestUrl(Server $server)
99
    {
100
        $requestUrl = parent::requestUrl($server);
101
102
        return $requestUrl.'/'.$this->siteResourcePath();
103
    }
104
105
    /**
106
     * Processes new response item.
107
     *
108
     * @param mixed $item
109
     *
110
     * @return mixed
111
     */
112
    public function processResponseItem($item)
113
    {
114
        return $item->setSite($this->getSite());
115
    }
116
117
    /**
118
     * Handle command response.
119
     *
120
     * @param \Psr\Http\Message\ResponseInterface $response
121
     * @param \Laravel\Forge\Server               $server
122
     *
123
     * @return bool|string
124
     */
125
    public function handleResponse(ResponseInterface $response, Server $server)
126
    {
127
        $siteResourceClass = $this->siteResourceClass();
128
129
        if (is_null($siteResourceClass)) {
130
            return true;
131
        }
132
133
        return parent::handleResponse($response, $server);
134
    }
135
}
136