Passed
Push — master ( ef19e3...1810ac )
by Korotkov
16:58 queued 08:39
created

CommonHelper::redirectToSelf()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 11
rs 10
c 0
b 0
f 0
cc 3
nc 3
nop 2
1
<?php
2
3
namespace App\Common;
4
5
trait CommonHelper
6
{
7
    protected function getPaginationLinks($page, $perPage, $numRows = null) :array
8
    {
9
        $this->setPagination($page, $perPage, isset($numRows) ? $numRows : $this->model()->numRows());
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? ( Ignorable by Annotation )

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

9
        $this->/** @scrutinizer ignore-call */ 
10
               setPagination($page, $perPage, isset($numRows) ? $numRows : $this->model()->numRows());
Loading history...
10
11
        return $this->pagination()->getLinks();
0 ignored issues
show
Bug introduced by
It seems like pagination() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

11
        return $this->/** @scrutinizer ignore-call */ pagination()->getLinks();
Loading history...
12
    }
13
14
    protected function getPaginationViewData($page, $limit, $uri, &$links) :array
15
    {
16
        return [
17
            'limit' => $limit,
18
            'page'  => $page['id'],
19
            'uri'   => $uri,
20
            'links' => $links
21
        ];
22
    }
23
24
    protected function validationErrors(array $result, string $type = 'alert') :void
25
    {
26
        if ($type == 'API') {
27
            $this->container()->jsonResponse($this->validation()->flash($result, []));
0 ignored issues
show
Bug introduced by
The method validation() does not exist on App\Common\CommonHelper. Did you maybe mean validationErrors()? ( Ignorable by Annotation )

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

27
            $this->container()->jsonResponse($this->/** @scrutinizer ignore-call */ validation()->flash($result, []));

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug introduced by
It seems like container() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

27
            $this->/** @scrutinizer ignore-call */ 
28
                   container()->jsonResponse($this->validation()->flash($result, []));
Loading history...
28
            exit();
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
29
        }
30
31
        $data = ($type == 'value') ? $this->validation()->get($result, ['csrf_field'])
32
            : $this->validation()->flash($result, ['csrf_field']);
33
34
        foreach ($data as $key => $message) {
35
            $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? ( Ignorable by Annotation )

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

35
            $this->/** @scrutinizer ignore-call */ 
36
                   setSession($type, $message, $key);
Loading history...
36
        }
37
    }
38
39
    protected function getIdFromSlug(string $slug) :string
40
    {
41
        $slug = strip_tags($slug);
42
43
        return (strpos($slug, '_') !== false) ? strstr($slug, '_', true) : $slug;
44
    }
45
46
    protected function redirectToSelf(string $uri, $page = null)
47
    {
48
        if (isset($page)) {
49
            $page = $this->validation()->sanitize($page)->required()->run();
50
51
            if ($this->validation()->access($page)) {
52
                return $this->redirect($uri . '/page/' . $page[0]);
0 ignored issues
show
Bug introduced by
The method redirect() does not exist on App\Common\CommonHelper. Did you maybe mean redirectToSelf()? ( Ignorable by Annotation )

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

52
                return $this->/** @scrutinizer ignore-call */ redirect($uri . '/page/' . $page[0]);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
53
            }
54
        }
55
56
        return $this->redirect($uri);
57
    }
58
59
    protected function searchArrayKey($array, $arrayKey)
60
    {
61
        foreach ($array as $key => $value) {
62
            if (stristr($key, $arrayKey) !== false) {
63
                return $key;
64
            }
65
        }
66
    }
67
68
    protected function dateFormat($value)
69
    {
70
        $date  = \DateTime::createFromFormat('d.m.Y H:i', $value);
71
        $value = $date->format('Y-m-d H:i');
72
73
        return $value;
74
    }
75
76
    protected function checkbox($value)
77
    {
78
        return ($value) ? '1' : '0';
79
    }
80
81
    public function model()
82
    {
83
        return $this->model;
84
    }
85
86
    public function setModel(string $modelName): void
87
    {
88
        $this->model = $this->container()->new($modelName);
0 ignored issues
show
Bug Best Practice introduced by
The property model does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
89
    }
90
}
91