Completed
Push — development ( eb9524...db4517 )
by Andrij
28:49 queued 02:09
created

BannerPageTypesManager   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 109
Duplicated Lines 22.02 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 15
c 1
b 0
f 0
lcom 1
cbo 0
dl 24
loc 109
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getInstance() 0 6 2
B getPagesTypes() 0 34 2
B getView() 12 12 5
B getPages() 12 12 5

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
namespace xbanners\src\Managers;
3
4
class BannerPageTypesManager
0 ignored issues
show
Coding Style introduced by
Since you have declared the constructor as private, maybe you should also declare the class as final.
Loading history...
5
{
0 ignored issues
show
introduced by
Opening brace of a class must be on the same line as the definition
Loading history...
6
7
    const PAGE_TYPE_MAIN = 'main';
8
    const PAGE_TYPE_SHOP_CATEGORY = 'shop_category';
9
    const PAGE_TYPE_PRODUCT = 'product';
10
    const PAGE_TYPE_CATEGORY = 'category';
11
    const PAGE_TYPE_PAGE = 'page';
12
    const PAGE_TYPE_BRAND = 'brand';
13
    const PAGE_TYPE_SEARCH = 'search';
14
15
    /**
16
     * @var BannerPageTypesManager instance
17
     */
18
    private static $instance = NULL;
19
20
    private function __construct() {
21
22
    }
23
24
    /**
25
     * Get BannerPageTypesManager instance
26
     * @return BannerPageTypesManager instance
27
     */
28
    public static function getInstance() {
29
        if (null === self::$instance) {
30
            self::$instance = new self();
31
        }
32
        return self::$instance;
33
    }
34
35
    /**
36
     * Get banner pages type|types: ['main', 'shop_category', 'product',...] on nothing to get all types array
37
     * @param string $type - banner type
0 ignored issues
show
Documentation introduced by
Should the type for parameter $type not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
38
     * @return array
39
     */
40
    public function getPagesTypes($type = NULL) {
41
        $pageTypes = [
42
            'main' => [
43
                'name' => lang('Main', 'xbanners'),
44
                'class' => NULL
45
            ],
46
            'shop_category' => [
47
                'name' => lang('Product category', 'xbanners'),
48
                'class' => '\xbanners\src\BannerPagesTypes\ShopCategory'
49
            ],
50
            'product' => [
51
                'name' => lang('Product', 'xbanners'),
52
                'class' => '\xbanners\src\BannerPagesTypes\Product'
53
            ],
54
            'category' => [
55
                'name' => lang('Page category', 'xbanners'),
56
                'class' => '\xbanners\src\BannerPagesTypes\Category'
57
            ],
58
            'page' => [
59
                'name' => lang('Page', 'xbanners'),
60
                'class' => '\xbanners\src\BannerPagesTypes\Page'
61
            ],
62
            'brand' => [
63
                'name' => lang('Product brand', 'xbanners'),
64
                'class' => '\xbanners\src\BannerPagesTypes\Brand'
65
            ],
66
            'search' => [
67
                'name' => lang('Search page', 'xbanners'),
68
                'class' => NULL
69
            ],
70
        ];
71
72
        return $type ? $pageTypes[$type] : $pageTypes;
73
    }
74
75
    /**
76
     * Get banner type view
77
     * @param string $type - banner page type name type: ['main', 'shop_category', 'product',...]
78
     * @param string $locale - locale name
0 ignored issues
show
Documentation introduced by
Should the type for parameter $locale not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
79
     * @return array
80
     */
81 View Code Duplication
    public function getView($type, $locale = NULL) {
82
        $locale = $locale ? $locale : \MY_Controller::defaultLocale();
83
        $type = $this->getPagesTypes($type);
84
85
        if (!$type OR !$type['class'] OR !class_exists($type['class'])) {
86
            return NULL;
87
        }
88
89
        $typeObj = new $type['class']($locale);
90
91
        return $typeObj->getView();
92
    }
93
94
    /**
95
     * Get banner page type allowed pages list
96
     * @param string $type - banner page type name type: ['main', 'shop_category', 'product',...]
97
     * @param string $locale - locale name
0 ignored issues
show
Documentation introduced by
Should the type for parameter $locale not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
98
     * @return array
99
     */
100 View Code Duplication
    public function getPages($type, $locale = NULL) {
101
        $locale = $locale ? $locale : \MY_Controller::defaultLocale();
102
        $type = $this->getPagesTypes($type);
103
104
        if (!$type OR !$type['class'] OR !class_exists($type['class'])) {
105
            return NULL;
106
        }
107
108
        $typeObj = new $type['class']($locale);
109
110
        return $typeObj->getPages();
111
    }
112
}