Completed
Push — master ( 44bad9...3ecbe1 )
by Jan
04:21
created

ColumnSecurity::getPlaceholder()   C

Complexity

Conditions 13
Paths 12

Size

Total Lines 38
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 13
eloc 27
c 3
b 0
f 0
nc 12
nop 0
dl 0
loc 38
rs 6.6166

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * part-db version 0.1
4
 * Copyright (C) 2005 Christoph Lechner
5
 * http://www.cl-projects.de/.
6
 *
7
 * part-db version 0.2+
8
 * Copyright (C) 2009 K. Jacobs and others (see authors.php)
9
 * http://code.google.com/p/part-db/
10
 *
11
 * Part-DB Version 0.4+
12
 * Copyright (C) 2016 - 2019 Jan Böhmer
13
 * https://github.com/jbtronics
14
 *
15
 * This program is free software; you can redistribute it and/or
16
 * modify it under the terms of the GNU General Public License
17
 * as published by the Free Software Foundation; either version 2
18
 * of the License, or (at your option) any later version.
19
 *
20
 * This program is distributed in the hope that it will be useful,
21
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23
 * GNU General Public License for more details.
24
 *
25
 * You should have received a copy of the GNU General Public License
26
 * along with this program; if not, write to the Free Software
27
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
28
 */
29
30
namespace App\Security\Annotations;
31
32
use App\Entity\Base\NamedDBElement;
33
use Doctrine\Common\Annotations\Annotation;
34
use Doctrine\Common\Collections\ArrayCollection;
35
use \InvalidArgumentException;
36
37
/**
38
 * @Annotation
39
 *
40
 * @Annotation\Target("PROPERTY")
41
 *
42
 * With these annotation you can restrict the access to certain coloumns in entities.
43
 * The entity which should use this class has to use ElementListener as EntityListener.
44
 */
45
class ColumnSecurity
46
{
47
    /**
48
     * @var string The name of the edit permission
49
     */
50
    public $edit = 'edit';
51
    /**
52
     * @var string The name of the read permission
53
     */
54
    public $read = 'read';
55
56
    /**
57
     * @var string A prefix for all permission names (e.g..edit, useful for Parts)
58
     */
59
    public $prefix = '';
60
61
    /**
62
     * @var string The placeholder that should be used, when the access to the property is denied.
63
     */
64
    public $placeholder = null;
65
66
    public $subject = null;
67
68
    /**
69
     * @var string The name of the property. This is used to determine the default placeholder.
70
     * @Annotation\Enum({"integer", "string", "object", "boolean", "datetime", "collection"})
71
     */
72
    public $type = 'string';
73
74
    public function getReadOperationName(): string
75
    {
76
        if ('' !== $this->prefix) {
77
            return $this->prefix.'.'.$this->read;
78
        }
79
80
        return $this->read;
81
    }
82
83
    public function getEditOperationName(): string
84
    {
85
        if ('' !== $this->prefix) {
86
            return $this->prefix.'.'.$this->edit;
87
        }
88
89
        return $this->edit;
90
    }
91
92
    public function getPlaceholder()
93
    {
94
        //Check if a class name was specified
95
        if (class_exists($this->type)) {
96
            $object = new $this->type();
97
            if ($object instanceof NamedDBElement) {
98
                if (is_string($this->placeholder) && $this->placeholder !== "") {
99
                    $object->setName($this->placeholder);
100
                }
101
                $object->setName('???');
102
            }
103
            return $object;
104
        }
105
106
107
        if (null === $this->placeholder) {
108
            switch ($this->type) {
109
                case 'integer':
110
                    return 0;
111
                case 'float':
112
                    return 0.0;
113
                case 'string':
114
                    return '???';
115
                case 'object':
116
                    return null;
117
                case 'collection':
118
                    return new ArrayCollection();
119
                case 'boolean':
120
                    return false;
121
                case 'datetime':
122
                    $date = new \DateTime();
123
                    return $date->setTimestamp(0);
124
                default:
125
                    throw new InvalidArgumentException('Unknown type! You have to specify a placeholder!');
126
            }
127
        }
128
129
        return $this->placeholder;
130
    }
131
}
132