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

TemplatePlacesInstaller   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 3
Bugs 1 Features 0
Metric Value
wmc 10
lcom 1
cbo 4
dl 0
loc 67
rs 10
c 3
b 1
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 2
A install() 0 13 2
A update() 0 14 3
A dropAll() 0 3 1
A getBanners() 0 6 2
1
<?php
2
3
namespace xbanners\src\Installers;
4
5
use xbanners\models\Banners;
6
use xbanners\models\BannersQuery;
7
use template_manager\classes\TemplateManager;
8
use xbanners\src\Entities\BannerEffects;
9
10
/**
11
 * Install all banners from temlate
12
 *
13
 * @author Crayd
14
 */
15
final class TemplatePlacesInstaller
16
{
0 ignored issues
show
introduced by
Opening brace of a class must be on the same line as the definition
Loading history...
17
18
    /**
19
     * @var string
20
     */
21
    protected $bannersConfigFile;
22
23
    /**
24
     * @param string|void $templateName installed tamplate name
25
     */
26
    public function __construct($templateName = null) {
27
        if (!$templateName) {
28
            $templateName = TemplateManager::getInstance()->getCurentTemplate()->name;
29
        }
30
        $this->bannersConfigFile = TEMPLATES_PATH . $templateName . '/xbanners/banner_places.php';
31
    }
32
33
    /**
34
     * Save all tamplate banners to databse
35
     */
36
    public function install() {
37
        $this->dropAll();
38
        foreach ($this->getBanners() as $key => $banner) {
39
            $bannerModel = new Banners();
40
            $bannerModel->setPlace($key);
41
            $bannerModel->setName($banner['name']);//for i18n
42
            $bannerModel->fromArray($banner, \Propel\Runtime\Map\TableMap::TYPE_FIELDNAME);
43
44
            $bannerModel->setEffects((new BannerEffects($banner['effects'], TRUE))->toArray());//default effects
45
46
            $bannerModel->save();
47
        }
48
    }
49
50
    public function update() {
51
        foreach ($this->getBanners() as $place => $banner) {
52
            $bannerModel = BannersQuery::create()->findOneByPlace($place);
53
            if (!count($bannerModel)) {
54
                $bannerModel = new Banners();
55
                $bannerModel->setPlace($place);
56
                $bannerModel->setName($banner['name']);//for i18n
57
                $bannerModel->setEffects((new BannerEffects($banner['effects'], TRUE))->toArray());//default effects
58
            }
59
            $bannerModel->fromArray($banner, \Propel\Runtime\Map\TableMap::TYPE_FIELDNAME);
60
61
            $bannerModel->save();
62
        }
63
    }
64
65
    public function dropAll() {
66
        BannersQuery::create()->deleteAll();
67
    }
68
69
    /**
70
     * Get template banners
71
     *
72
     * @return array
73
     * @throws \Exception
0 ignored issues
show
introduced by
Comment missing or not on the next line for @throws tag in function comment
Loading history...
74
     */
75
    protected function getBanners() {
76
        if (file_exists($this->bannersConfigFile)) {
77
            return include $this->bannersConfigFile;
78
        }
79
        throw new \Exception(lang('Could not find banner settings', 'xbanners'));
80
    }
81
82
}