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

ClickStatistic::goToUrl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace xbanners\src\Statistic;
4
5
use xbanners\models\BannerImageQuery;
6
7
/**
8
 * Click Statistic class
9
 */
10
class ClickStatistic
11
{
0 ignored issues
show
introduced by
Opening brace of a class must be on the same line as the definition
Loading history...
12
13
    /**
14
     * Banner image object
15
     * @var array|\Banners\Models\BannerImage|mixed
16
     */
17
    private $bannerImage;
18
19
    public function __construct($imageIdMd5, $locale = NULL) {
20
21
        $locale = $locale ? $locale : \MY_Controller::getCurrentLocale();
22
23
        $this->bannerImage = BannerImageQuery::create()
24
            ->joinWithI18n($locale)
25
            ->where('md5(BannerImage.Id) = ?', $imageIdMd5)
26
            ->findOne();
27
    }
28
29
    /**
30
     * Run click statistic
31
     */
32
    public function run() {
33
34
        if ($this->bannerImage->getUrl()) {
35
            $this->increaseClicks();
36
            $this->goToUrl();
37
        } else {
38
            \CI::$APP->core->error_404();
39
        }
40
    }
41
42
    /**
43
     * Increase banner image clicks value
44
     * @throws \Propel\Runtime\Exception\PropelException
0 ignored issues
show
introduced by
Comment missing or not on the next line for @throws tag in function comment
Loading history...
45
     */
46
    public function increaseClicks() {
47
48
        $this->bannerImage->setClicks($this->bannerImage->getClicks() + 1);
49
        $this->bannerImage->save();
50
    }
51
52
    /**
53
     * Go to banner image url
54
     */
55
    private function goToUrl() {
56
57
        redirect($this->bannerImage->getUrl(), 'location', '301');
58
    }
59
60
    /**
61
     * Get click statistic
62
     * @param $imageId - banner image id
0 ignored issues
show
introduced by
Missing parameter type
Loading history...
63
     * @return string
64
     */
65
    public static function getUrl($imageId) {
66
67
        return site_url('xbanners/go') . '/' . md5($imageId);
68
    }
69
70
}