Completed
Push — master ( f92166...9df481 )
by Korotkov
08:19
created

CommonHelper::getPaginationLinks()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 3
1
<?php
2
3
namespace App\Http\Supports;
4
5
trait CommonHelper
6
{
7
8
    /**
9
     * @param      $page
10
     * @param      $perPage
11
     * @param null $numRows
12
     *
13
     * @return array
14
     */
15
    protected function getPaginationLinks($page, $perPage, $numRows = null) :array
16
    {
17
        $numRows = isset($numRows) ? $numRows : $this->model()->numRows();
0 ignored issues
show
Bug introduced by
It seems like model() 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...
18
19
        $this->setPagination($page);
0 ignored issues
show
Bug introduced by
It seems like setPagination() 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...
20
        $this->pagination()->setPerPage($perPage);
0 ignored issues
show
Bug introduced by
The method pagination() does not exist on App\Http\Supports\CommonHelper. Did you maybe mean getPaginationLinks()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
21
        $this->pagination()->setCount($numRows);
0 ignored issues
show
Bug introduced by
The method pagination() does not exist on App\Http\Supports\CommonHelper. Did you maybe mean getPaginationLinks()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
22
23
        return $this->pagination()->getLinks();
0 ignored issues
show
Bug introduced by
The method pagination() does not exist on App\Http\Supports\CommonHelper. Did you maybe mean getPaginationLinks()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
24
    }
25
26
    /**
27
     * @param $page
28
     * @param $limit
29
     * @param $uri
30
     * @param $links
31
     *
32
     * @return array
33
     */
34
    protected function getPaginationViewData($page, $limit, $uri, &$links) :array
35
    {
36
        return [
37
            'limit' => $limit,
38
            'page'  => $page['id'],
39
            'uri'   => $uri,
40
            'links' => $links
41
        ];
42
    }
43
44
    /**
45
     * @param array  $result
46
     * @param string $type
47
     */
48
    protected function validationErrors(array $result, string $type = 'alert') :void
49
    {
50
        if ($type == 'API') {
51
            exit($this->container()->jsonResponse($this->validation()->flash($result, [])));
52
        }
53
54
        $data = ($type == 'value') ? $this->validation()->get($result, ['csrf_field'])
0 ignored issues
show
Bug introduced by
The method validation() does not exist on App\Http\Supports\CommonHelper. Did you maybe mean validationErrors()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
55
            : $this->validation()->flash($result, ['csrf_field']);
0 ignored issues
show
Bug introduced by
The method validation() does not exist on App\Http\Supports\CommonHelper. Did you maybe mean validationErrors()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
56
57
        foreach ($data as $key => $message) {
58
            $this->setSession($type, $message, $key);
0 ignored issues
show
Bug introduced by
It seems like setSession() 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...
59
        }
60
    }
61
62
    /**
63
     * @param $slug
64
     *
65
     * @return string
66
     */
67
    protected function getIdFromSlug(string $slug) :string
68
    {
69
        $slug = strip_tags($slug);
70
71
        return (strpos($slug, '_') !== false) ? strstr($slug, '_', true) : $slug;
72
    }
73
74
    /**
75
     * @param string $uri
76
     *
77
     * @return mixed
78
     */
79
    protected function redirectToSelf(string $uri)
80
    {
81
        $page = $this->validation()->sanitize($this->post('page'))->required()->run();
0 ignored issues
show
Bug introduced by
The method validation() does not exist on App\Http\Supports\CommonHelper. Did you maybe mean validationErrors()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
Bug introduced by
It seems like post() 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...
82
83
        if ($this->validation()->access($page)) {
0 ignored issues
show
Bug introduced by
The method validation() does not exist on App\Http\Supports\CommonHelper. Did you maybe mean validationErrors()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
84
            return $this->redirect($uri . '/page/' . $page[0]);
0 ignored issues
show
Bug introduced by
The method redirect() does not exist on App\Http\Supports\CommonHelper. Did you maybe mean redirectToSelf()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
85
        }
86
87
        return $this->redirect($uri);
0 ignored issues
show
Bug introduced by
The method redirect() does not exist on App\Http\Supports\CommonHelper. Did you maybe mean redirectToSelf()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
88
    }
89
90
    /**
91
     * @param $array
92
     * @param $arrayKey
93
     *
94
     * @return int|string
95
     */
96
    protected function searchArrayKey($array, $arrayKey)
97
    {
98
        foreach ($array as $key => $value) {
99
            if (stristr($key, $arrayKey) !== false) {
100
                return $key;
101
            }
102
        }
103
    }
104
105
    /**
106
     * @param $value
107
     *
108
     * @return string
109
     */
110
    protected function dateFormat($value)
111
    {
112
        $date  = \DateTime::createFromFormat('d.m.Y H:i', $value);
113
        $value = $date->format('Y-m-d H:i');
114
115
        return $value;
116
    }
117
118
    /**
119
     * @param $value
120
     *
121
     * @return int
122
     */
123
    protected function checkbox($value)
124
    {
125
        return ($value) ? '1' : '0';
126
    }
127
}