Completed
Push — experimental/sf ( 7e7cd6...2a520b )
by
unknown
273:54 queued 265:51
created

EntityUtil   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 89.47%

Importance

Changes 0
Metric Value
dl 0
loc 75
rs 10
c 0
b 0
f 0
ccs 17
cts 19
cp 0.8947
wmc 7
lcom 0
cbo 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
A isEmpty() 0 14 3
A isNotEmpty() 0 4 1
A dumpToArray() 0 14 3
1
<?php
2
3
/*
4
 * This file is part of EC-CUBE
5
 *
6
 * Copyright(c) LOCKON CO.,LTD. All Rights Reserved.
7
 *
8
 * http://www.lockon.co.jp/
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Eccube\Util;
15
16
use Doctrine\ORM\EntityNotFoundException;
17
use Doctrine\ORM\Proxy\Proxy;
18
19
class EntityUtil
0 ignored issues
show
introduced by
Missing class doc comment
Loading history...
20
{
21
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$entity" missing
Loading history...
22
     * LAZY loading したエンティティの有無をチェックする.
23
     *
24
     * 削除済みのエンティティを LAZY loading した場合、 soft_delete filter で
25
     * フィルタリングされてしまい、正常に取得することができない.
26
     * しかし、 Proxy オブジェクトとして取得されるため、この関数を使用して
27
     * 有無をチェックする.
28
     * この関数を使用せず、該当のオブジェクトのプロパティを取得しようとすると、
29
     * EntityNotFoundException がスローされてしまう.
30
     *
31
     * @param $entity LAZY loading したエンティティ
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
32
     *
33
     * @return bool エンティティが削除済みの場合 true
34
     *
35
     * @see https://github.com/EC-CUBE/ec-cube/pull/602#issuecomment-125431246
36
     */
37 5
    public static function isEmpty($entity)
38
    {
39 5
        if ($entity instanceof Proxy) {
40
            try {
41 1
                $entity->__load();
42
            } catch (EntityNotFoundException $e) {
43
                return true;
44
            }
45
46 1
            return false;
47
        } else {
48 5
            return empty($entity);
49
        }
50
    }
51
52
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$entity" missing
Loading history...
53
     * LAZY loading したエンティティの有無をチェックする.
54
     *
55
     * EntityUtil::isEmpty() の逆の結果を返します.
56
     *
57
     * @param $entity
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
58
     *
59
     * @return bool
60
     *
61
     * @see EntityUtil::isEmpty()
62
     */
63 4
    public static function isNotEmpty($entity)
64
    {
65 4
        return !self::isEmpty($entity);
66
    }
67
68
    /**
69
     * エンティティのプロパティを配列で返す.
70
     *
71
     * このメソッドはエンティティの内容をログ出力する際などに使用する.
72
     * AbstractEntity::toArray() と異なり再帰処理しない.
73
     * プロパティの値がオブジェクトの場合は、クラス名を出力する.
74
     *
75
     * @param object $entity 対象のエンティティ
76
     *
77
     * @return array エンティティのプロパティの配列
78
     */
79 1
    public static function dumpToArray($entity)
80
    {
81 1
        $objReflect = new \ReflectionClass($entity);
82 1
        $arrProperties = $objReflect->getProperties();
83 1
        $arrResults = [];
84 1
        foreach ($arrProperties as $objProperty) {
85 1
            $objProperty->setAccessible(true);
86 1
            $name = $objProperty->getName();
87 1
            $value = $objProperty->getValue($entity);
88 1
            $arrResults[$name] = is_object($value) ? get_class($value) : $value;
89
        }
90
91 1
        return $arrResults;
92
    }
93
}
94