ForgeSync::sites()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 1
nop 0
dl 0
loc 20
rs 9.6
c 0
b 0
f 0
1
<?php
2
3
namespace OhDear\ForgeSync;
4
5
use OhDear\PhpSdk\OhDear;
6
use Themsaid\Forge\Forge;
7
use Illuminate\Support\Collection;
8
use Themsaid\Forge\Resources\Server;
9
use OhDear\PhpSdk\Resources\Site as OhDearSite;
10
use Themsaid\Forge\Resources\Site as ForgeSite;
11
12
class ForgeSync
13
{
14
    /** @var \Themsaid\Forge\Forge */
15
    protected $forge;
16
17
    /** @var \OhDear\PhpSdk\OhDear */
18
    protected $ohDear;
19
20
    /** @var int */
21
    protected $ohDearTeamId;
22
23
    public function __construct(int $ohDearTeamId, string $ohDearApiToken = null, string $forgeApiToken = null)
24
    {
25
        $this->ohDearTeamId = $ohDearTeamId;
26
27
        $this->ohDear = new OhDear($ohDearApiToken ?? config('forge-sync.ohdear_api_token'));
28
29
        $this->forge = new Forge($forgeApiToken ?? config('forge-sync.forge_api_token'));
30
    }
31
32
    public function sites(): Collection
33
    {
34
        $ohDearSites = collect($this->ohDear->sites())->map(function (OhDearSite $ohDearSite) {
35
            return $ohDearSite->url;
36
        })->toArray();
37
38
        return collect($this->forge->servers())->flatMap(function (Server $server) {
39
            return collect($this->forge->sites($server->id))->filter(function (ForgeSite $forgeSite) {
40
                return $forgeSite->name !== 'default' && $forgeSite->name !== '';
41
            })->map(function ($forgeSite) {
42
                return new Site($this->forge->siteNginxFile($forgeSite->serverId, $forgeSite->id));
43
            });
44
        })
45
        ->filter(function (Site $site) {
46
            return $site->shouldBeMonitoredByOhDear();
47
        })
48
        ->filter(function (Site $site) use ($ohDearSites) {
49
            return in_array($site->url(), $ohDearSites) == false;
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
50
        });
51
    }
52
53
    public function addToOhDear(Site $site)
54
    {
55
        return $this->ohDear->createSite(['url' => $site->url(), 'team_id' => $this->ohDearTeamId]);
56
    }
57
}
58