PageService   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 2
dl 0
loc 77
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 2
B set() 0 18 5
A get() 0 4 1
A getBySlug() 0 6 1
A getVisibleBySlug() 0 6 1
1
<?php
2
3
namespace Ngtfkx\Laradeck\Pages;
4
5
use Ngtfkx\Laradeck\Pages\Exceptions\PageNotFoundException;
6
use Ngtfkx\Laradeck\Pages\Models\Page;
7
8
class PageService
9
{
10
    /**
11
     * @var Page
12
     */
13
    protected $page;
14
15
    /**
16
     * @param Page|int|string|null $page Модель, ID или slug
17
     */
18
    public function __construct($page = null)
19
    {
20
        if (!empty($page)) {
21
            $this->set($page);
22
        } else {
23
            $this->page = new Page;
24
        }
25
    }
26
27
    /**
28
     * @param $page Page|int|string Модель, ID или slug
29
     * @return PageService
30
     * @throws PageNotFoundException
31
     */
32
    public function set($page): PageService
33
    {
34
        if ($page instanceof Page) {
35
            $this->page = $page;
36
        } else {
37
            $object = (is_numeric($page) && $page == (int)$page)
38
                ? Page::find($page)
39
                : $this->getBySlug($page);
40
41
            if (empty($object)) {
42
                $message = sprintf('Страница не найдена (%s)', $page);
43
                throw new PageNotFoundException($message);
44
            }
45
46
            $this->page = $object;
47
        }
48
        return $this;
49
    }
50
51
    /**
52
     * @return Page
53
     */
54
    public function get(): Page
55
    {
56
        return $this->page;
57
    }
58
59
    /**
60
     * Получить страницу по его слагу
61
     *
62
     * @param string $slug
63
     * @return Page
64
     */
65
    public function getBySlug(string $slug)
66
    {
67
        $page = Page::bySlug($slug)->first();
68
69
        return $page;
70
    }
71
72
    /**
73
     * Получить страницу доступную для отображения по его слагу
74
     *
75
     * @param string $slug
76
     * @return Page
77
     */
78
    public function getVisibleBySlug(string $slug)
79
    {
80
        $page = Page::bySlug($slug)->active()->first();
81
82
        return $page;
83
    }
84
}