Completed
Push — master ( 21369d...7d30ed )
by Vitaly
02:24
created

Renderable::pager()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 2
eloc 4
nc 2
nop 2
1
<?php
2
/**
3
 * Created by Vitaly Iegorov <[email protected]>.
4
 * on 19.03.16 at 16:02
5
 */
6
namespace samsoncms\api;
7
8
use samsonframework\core\ViewInterface;
9
use samsonframework\view\View;
10
11
define('RENDERABLE_ITEMS_VARIABLE', 'items');
12
define('RENDERABLE_ITEM_VARIABLE', 'item');
13
14
/**
15
 * Generic renderable methods and variables.
16
 *
17
 * @package samsoncms\api
18
 */
19
trait Renderable
20
{
21
    /** @var string|callable Block view file or callback */
22
    protected $indexView;
23
24
    /** @var string|callable Item view file or callback */
25
    protected $itemView;
26
27
    /** @var string|callable Empty view file or callback */
28
    protected $emptyView = 'www/empty';
29
30
    /** @var ViewInterface View render object */
31
    protected $renderer;
32
33
    /** @var int Count of entities on one page */
34
    protected $pageSize;
35
36
    /** @var int Current page number */
37
    protected $pageNumber;
38
39
    /**
40
     * Set pagination for SamsonCMS query.
41
     *
42
     * @param int      $pageNumber Result page number
43
     * @param int|null $pageSize   Results page size
44
     *
45
     * @return $this Chaining
46
     */
47
    public function pager($pageNumber, $pageSize = null)
48
    {
49
        $this->pageNumber = $pageNumber;
50
        $this->pageSize = null !== $pageSize ? $pageSize : $this->pageSize;
51
52
        return $this;
53
    }
54
55
    /**
56
     * Set index view path.
57
     *
58
     * @param string|callable|View $indexView Index view path or callback
59
     *
60
     * @return $this Chaining
61
     */
62
    public function indexView($indexView)
63
    {
64
        $this->indexView = $indexView;
65
        return $this;
66
    }
67
68
    /**
69
     * Set item view path.
70
     *
71
     * @param string|callable|View $itemView Item view path or callback
72
     *
73
     * @return $this Chaining
74
     */
75
    public function itemView($itemView)
76
    {
77
        $this->itemView = $itemView;
78
79
        return $this;
80
    }
81
82
    /**
83
     * Set empty view path.
84
     *
85
     * @param string|callable|View $emptyView Empty view path or callback
86
     *
87
     * @return $this Chaining
88
     */
89
    public function emptyView($emptyView)
90
    {
91
        $this->emptyView = $emptyView;
92
        return $this;
93
    }
94
95
    /**
96
     * Render Entity collection item.
97
     *
98
     * @param Entity $item SamsonCMS entity for rendering
99
     *
100
     * @return string Rendered HTML
101
     */
102
    public function renderItem(Entity $item)
103
    {
104
        // Set correct renderer old style or new \samsonframework\view
105
        $renderer = ($this->itemView instanceof View)
0 ignored issues
show
Bug introduced by
The class samsonframework\view\View does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
106
            ? $this->itemView
107
            : $this->renderer->view($this->itemView);
108
109
        return $renderer
110
            ->set($item, RENDERABLE_ITEM_VARIABLE)
111
            ->output();
112
113
    }
114
115
    /**
116
     * Render empty collection item.
117
     *
118
     * @return string Rendered HTML
119
     */
120
    public function renderEmpty()
121
    {
122
        // Set correct renderer old style or new \samsonframework\view
123
        $renderer = ($this->emptyView instanceof View)
0 ignored issues
show
Bug introduced by
The class samsonframework\view\View does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
124
            ? $this->emptyView
125
            : $this->renderer->view($this->emptyView);
126
127
        return $renderer->output();
128
    }
129
130
    /**
131
     * Render Entity collection index.
132
     *
133
     * @param string $items Collection of rendered items
134
     *
135
     * @return string Rendered HTML
136
     */
137
    public function renderIndex($items)
138
    {
139
        // Set correct renderer old style or new \samsonframework\view
140
        $renderer = ($this->indexView instanceof View)
0 ignored issues
show
Bug introduced by
The class samsonframework\view\View does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
141
            ? $this->indexView
142
            : $this->renderer->view($this->indexView);
143
144
        return $renderer
145
            ->set($items, RENDERABLE_ITEMS_VARIABLE)
146
            ->output();
147
    }
148
149
    /** @return string Rendered fields table */
150
    public function __toString()
151
    {
152
        return $this->output();
153
    }
154
155
    /** @return string Rendered HTML for fields table */
156
    public function output()
157
    {
158
        // Perform SamsonCMS query
159
        $collection = $this->find($this->pageNumber, $this->pageSize);
0 ignored issues
show
Bug introduced by
It seems like find() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
160
161
        if (count($collection)) {
162
            // Render each entity view in collection
163
            $html = '';
164
            foreach ($collection as $row) {
165
                $html .= $this->innerRender($row, $collection, 'itemView', 'renderItem');
166
            }
167
168
            // Render collection main view with items
169
            return $this->innerRender($html, $collection, 'indexView', 'renderIndex');
170
        } else { // Render empty entity view
171
            return $this->innerRender('', $collection, 'emptyView', 'renderEmpty');
172
        }
173
    }
174
175
    /**
176
     * Generic view renderer.
177
     *
178
     * @param mixed    $item
179
     * @param Entity[] $collection
180
     * @param string   $variableName
181
     * @param string   $methodName
182
     *
183
     * @return mixed Rendered view
184
     */
185
    protected function innerRender($item, $collection, $variableName, $methodName)
186
    {
187
        return is_callable($this->$variableName)
188
            ? call_user_func($this->$variableName, $item, $this->renderer, $this->query, $collection)
0 ignored issues
show
Bug introduced by
The property query does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
189
            : $this->$methodName($item);
190
    }
191
}
192