Completed
Push — dev/product_visibility ( 1ee472 )
by Kiyotaka
06:23
created

PublishTermVisibility::checkVisibility()   A

Complexity

Conditions 6
Paths 5

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
nc 5
nop 1
dl 0
loc 14
rs 9.2222
c 0
b 0
f 0
1
<?php
2
/*
3
 * This file is part of EC-CUBE
4
 *
5
 * Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved.
6
 *
7
 * http://www.ec-cube.co.jp/
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
13
namespace Eccube\Service\Product;
14
15
16
use DateTime;
17
use Eccube\Doctrine\Query\WhereClause;
18
use Eccube\Entity\Product;
19
20
class PublishTermVisibility extends ProductVisibility
21
{
22
    public function checkVisibility(Product $Product)
23
    {
24
        $now = new DateTime();
25
26
        if ($Product->getPublishStart() && $Product->getPublishEnd()) {
27
            return $Product->getPublishStart() <= $now && $Product->getPublishEnd() > $now;
28
        } elseif ($Product->getPublishStart()) {
29
            return $Product->getPublishStart() <= $now;
30
        } elseif ($Product->getPublishEnd()) {
31
            return $Product->getPublishEnd() > $now;
32
        }
33
34
        return true;
35
    }
36
37
    protected function createStatements($params, $queryKey)
38
    {
39
        $now = new DateTime();
40
41
        $start = WhereClause::lt('p.publishStart', ':publishStart', $now);
42
        $end = WhereClause::gte('p.publishEnd', ':publishEnd', $now);
43
        $startIsNull = WhereClause::isNull('p.publishEnd');
44
        $endIsNull = WhereClause::isNull('p.publishStart');
45
46
        return [WhereClause::or(
47
            WhereClause::and($start, $end),
48
            WhereClause::and($start, $startIsNull),
49
            WhereClause::and($endIsNull, $end),
50
            WhereClause::and($startIsNull, $endIsNull)
51
        )];
52
    }
53
}
54