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

TemplatePlacesInstaller::install()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 13
rs 9.4285
cc 2
eloc 9
nc 2
nop 0
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
}