ServiceRepository::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: leo108
5
 * Date: 16/9/17
6
 * Time: 20:13
7
 */
8
9
namespace Leo108\CAS\Repositories;
10
11
use Leo108\CAS\Models\Service;
12
use Leo108\CAS\Models\ServiceHost;
13
14
class ServiceRepository
15
{
16
    /**
17
     * @var Service
18
     */
19
    protected $service;
20
21
    /**
22
     * @var ServiceHost;
23
     */
24
    protected $serviceHost;
25
26
    /**
27
     * ServiceRepository constructor.
28
     * @param Service     $service
29
     * @param ServiceHost $serviceHost
30
     */
31 27
    public function __construct(Service $service, ServiceHost $serviceHost)
32
    {
33 27
        $this->service     = $service;
34 27
        $this->serviceHost = $serviceHost;
35 27
    }
36
37
    /**
38
     * @param $url
39
     * @return Service|null
40
     */
41 1
    public function getServiceByUrl($url)
42
    {
43 1
        $host = parse_url($url, PHP_URL_HOST);
44
45 1
        $record = $this->serviceHost->where('host', $host)->first();
46 1
        if (!$record) {
47 1
            return null;
48
        }
49
50 1
        return $record->service;
51
    }
52
53
    /**
54
     * @param $url
55
     * @return bool
56
     */
57 1
    public function isUrlValid($url)
58
    {
59 1
        $service = $this->getServiceByUrl($url);
60
61 1
        return $service !== null && $service->enabled;
62
    }
63
}
64