PublicationQuery::filterByYear()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 8

Duplication

Lines 15
Ratio 100 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 15
loc 15
rs 9.4286
cc 3
eloc 8
nc 3
nop 1
1
<?php
2
3
namespace Fenrizbes\PublicationsBundle\Model;
4
5
use Fenrizbes\PublicationsBundle\Model\om\BasePublicationQuery;
6
use Symfony\Component\DependencyInjection\Container;
7
8
class PublicationQuery extends BasePublicationQuery
9
{
10
    /**
11
     * Applies base filters
12
     *
13
     * @param string|null $type_key
14
     * @return PublicationQuery
15
     */
16
    public function applyBaseFilter($type_key = null)
17
    {
18
        return $this
19
            ->filterByIsPublished(true)
20
            ->filterByCreatedAt(new \DateTime(), \Criteria::LESS_EQUAL)
21
22
            ->_if(!is_null($type_key))
23
                ->filterByPublicationTypeKey($type_key)
24
            ->_endif()
25
        ;
26
    }
27
28
    /**
29
     * Applies the sorting by date
30
     *
31
     * @param string|null $sort_order
32
     * @return PublicationQuery
33
     */
34
    public function applySorting($sort_order = null)
35
    {
36
        return $this
37
            ->_if(!is_null($sort_order))
38
                ->orderByCreatedAt($sort_order)
39
            ->_endif()
40
        ;
41
    }
42
43
    /**
44
     * Applies the random sorting
45
     *
46
     * @return PublicationQuery
47
     */
48
    public function sortRandomly()
49
    {
50
        return $this->addAscendingOrderByColumn('rand()');
51
    }
52
53
    /**
54
     * Applies the limitation and returns a publication or a collection of publications
55
     *
56
     * @param $limit
57
     * @return array|mixed|\PropelObjectCollection|Publication
58
     */
59
    public function findWithLimit($limit)
60
    {
61
        $this->limit($limit);
62
63
        if ($limit === 1) {
64
            return $this->findOne();
65
        }
66
67
        return $this->find();
68
    }
69
70
    /**
71
     * Applies the filter by year
72
     *
73
     * @param string|int $value
74
     * @return PublicationQuery
75
     * @throws \Exception
76
     */
77 View Code Duplication
    public function filterByYear($value)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
78
    {
79
        if (is_null($value)) {
80
            return $this;
81
        }
82
83
        if (!preg_match('/^\d{4}$/', $value)) {
84
            throw new \Exception('Incorrect year value: '. $value);
85
        }
86
87
        return $this->filterByCreatedAt(array(
88
            'min' => $value .'-01-01 00:00:00',
89
            'max' => $value .'-12-31 23:59:59'
90
        ));
91
    }
92
93
    /**
94
     * Applies the filter by month
95
     *
96
     * @param string $value
97
     * @return PublicationQuery
98
     * @throws \Exception
99
     */
100
    public function filterByMonth($value)
101
    {
102
        if (is_null($value)) {
103
            return $this;
104
        }
105
106
        if (!preg_match('/^\d{4}\-\d{2}$/', $value)) {
107
            throw new \Exception('Incorrect month value: '. $value);
108
        }
109
110
        $datetime = new \DateTime($value .'-01 00:00:00');
111
112
        return $this->filterByCreatedAt(array(
113
            'min' => $datetime->format('Y-m-d H:i:s'),
114
            'max' => $datetime->format('Y-m-t 23:59:59')
115
        ));
116
    }
117
118
    /**
119
     * Applies the filter by day
120
     *
121
     * @param string $value
122
     * @return PublicationQuery
123
     * @throws \Exception
124
     */
125 View Code Duplication
    public function filterByDay($value)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
126
    {
127
        if (is_null($value)) {
128
            return $this;
129
        }
130
131
        if (!preg_match('/^\d{4}\-\d{2}\-\d{2}$/', $value)) {
132
            throw new \Exception('Incorrect day value: '. $value);
133
        }
134
135
        return $this->filterByCreatedAt(array(
136
            'min' => $value .' 00:00:00',
137
            'max' => $value .' 23:59:59'
138
        ));
139
    }
140
}
141