PresenterRepository::getPresenter()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Yeelight\Repositories;
4
5
use Illuminate\Pagination\LengthAwarePaginator;
6
use Illuminate\Support\Collection;
7
use Prettus\Repository\Contracts\Presentable;
8
use Prettus\Repository\Contracts\PresenterInterface;
9
use Yeelight\Presenters\BasicPresenter;
10
11
/**
12
 * Class PresenterRepository
13
 *
14
 * @category Yeelight
15
 *
16
 * @package Yeelight\Repositories
17
 *
18
 * @author Sheldon Lee <[email protected]>
19
 *
20
 * @license https://opensource.org/licenses/MIT MIT
21
 *
22
 * @link https://www.yeelight.com
23
 */
24
class PresenterRepository
25
{
26
    protected $skipPresenter = false;
27
28
    protected $presenter = null;
29
30
    /**
31
     * PresenterRepository constructor.
32
     *
33
     * @param string|null $transformer transformer
34
     * @param bool $skipPresenter skipPresenter
35
     *
36
     * @throws \Exception
37
     */
38
    public function __construct(string $transformer = null, $skipPresenter = false)
39
    {
40
        $basicPresenter = new BasicPresenter();
41
        $basicPresenter->setTransformer($transformer);
0 ignored issues
show
Bug introduced by
It seems like $transformer can also be of type null; however, parameter $transformer of Yeelight\Presenters\Basi...enter::setTransformer() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

41
        $basicPresenter->setTransformer(/** @scrutinizer ignore-type */ $transformer);
Loading history...
42
        $this->presenter = $basicPresenter;
43
        $this->skipPresenter = $skipPresenter;
44
    }
45
46
    /**
47
     * IsSkipPresenter
48
     *
49
     * @return bool
50
     */
51
    public function isSkipPresenter(): bool
52
    {
53
        return $this->skipPresenter;
54
    }
55
56
    /**
57
     * @param bool $skipPresenter
58
     */
59
    public function setSkipPresenter(bool $skipPresenter)
60
    {
61
        $this->skipPresenter = $skipPresenter;
62
    }
63
64
    /**
65
     * @return null
66
     */
67
    public function getPresenter()
68
    {
69
        return $this->presenter;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->presenter returns the type Yeelight\Presenters\BasicPresenter which is incompatible with the documented return type null.
Loading history...
70
    }
71
72
    /**
73
     * @param null|PresenterInterface $presenter
74
     */
75
    public function setPresenter(PresenterInterface $presenter)
76
    {
77
        $this->presenter = $presenter;
78
    }
79
80
    /**
81
     * Wrapper result data.
82
     *
83
     * @param mixed $result
84
     *
85
     * @return mixed
86
     */
87
    public function parserResult($result)
88
    {
89
        if ($this->presenter instanceof PresenterInterface) {
0 ignored issues
show
introduced by
$this->presenter is always a sub-type of Prettus\Repository\Contracts\PresenterInterface.
Loading history...
90
            if ($result instanceof Collection || $result instanceof LengthAwarePaginator) {
91
                $result->each(function ($model) {
92
                    if ($model instanceof Presentable) {
93
                        $model->setPresenter($this->presenter);
94
                    }
95
96
                    return $model;
97
                });
98
            } elseif ($result instanceof Presentable) {
99
                $result = $result->setPresenter($this->presenter);
100
            }
101
102
            if (!$this->skipPresenter) {
103
                return $this->presenter->present($result);
0 ignored issues
show
Bug introduced by
It seems like $result can also be of type Illuminate\Pagination\LengthAwarePaginator; however, parameter $data of Yeelight\Base\Presenters\Presenter::present() does only seem to accept Illuminate\Support\Collection, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

103
                return $this->presenter->present(/** @scrutinizer ignore-type */ $result);
Loading history...
104
            }
105
        }
106
107
        return $result;
108
    }
109
}
110