ProjectDuplicationModel::getPossibleSelection()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 10

Duplication

Lines 13
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
eloc 10
nc 1
nop 0
dl 13
loc 13
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of Jitamin.
5
 *
6
 * Copyright (C) Jitamin Team
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Jitamin\Model;
13
14
use Jitamin\Foundation\Database\Model;
15
use Jitamin\Foundation\Security\Role;
16
17
/**
18
 * Project Duplication.
19
 */
20
class ProjectDuplicationModel extends Model
21
{
22
    /**
23
     * Get list of optional models to duplicate.
24
     *
25
     * @return string[]
26
     */
27 View Code Duplication
    public function getOptionalSelection()
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...
28
    {
29
        return [
30
            'categoryModel',
31
            'projectPermissionModel',
32
            'actionModel',
33
            'swimlaneModel',
34
            'tagDuplicationModel',
35
            'projectMetadataModel',
36
            'projectTaskDuplicationModel',
37
        ];
38
    }
39
40
    /**
41
     * Get list of all possible models to duplicate.
42
     *
43
     * @return string[]
44
     */
45 View Code Duplication
    public function getPossibleSelection()
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...
46
    {
47
        return [
48
            'boardModel',
49
            'categoryModel',
50
            'projectPermissionModel',
51
            'actionModel',
52
            'swimlaneModel',
53
            'tagDuplicationModel',
54
            'projectMetadataModel',
55
            'projectTaskDuplicationModel',
56
        ];
57
    }
58
59
    /**
60
     * Get a valid project name for the duplication.
61
     *
62
     * @param string $name       Project name
63
     * @param int    $max_length Max length allowed
64
     *
65
     * @return string
66
     */
67
    public function getClonedProjectName($name, $max_length = 50)
68
    {
69
        $suffix = ' ('.t('Clone').')';
70
71
        if (strlen($name.$suffix) > $max_length) {
72
            $name = substr($name, 0, $max_length - strlen($suffix));
73
        }
74
75
        return $name.$suffix;
76
    }
77
78
    /**
79
     * Clone a project with all settings.
80
     *
81
     * @param int    $src_project_id Project Id
82
     * @param array  $selection      Selection of optional project parts to duplicate
83
     * @param int    $owner_id       Owner of the project
84
     * @param string $name           Name of the project
85
     * @param bool   $private        Force the project to be private
86
     *
87
     * @return int Cloned Project Id
88
     */
89
    public function duplicate($src_project_id, $selection = ['projectPermissionModel', 'categoryModel', 'actionModel'], $owner_id = 0, $name = null, $private = null)
90
    {
91
        $this->db->startTransaction();
0 ignored issues
show
Documentation introduced by
The property db does not exist on object<Jitamin\Model\ProjectDuplicationModel>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
92
93
        // Get the cloned project Id
94
        $dst_project_id = $this->copy($src_project_id, $owner_id, $name, $private);
95
96
        if ($dst_project_id === false) {
97
            $this->db->cancelTransaction();
0 ignored issues
show
Documentation introduced by
The property db does not exist on object<Jitamin\Model\ProjectDuplicationModel>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
98
99
            return false;
100
        }
101
102
        // Clone Columns, Categories, Permissions and Actions
103
        foreach ($this->getPossibleSelection() as $model) {
104
105
            // Skip if optional part has not been selected
106
            if (in_array($model, $this->getOptionalSelection()) && !in_array($model, $selection)) {
107
                continue;
108
            }
109
110
            // Skip permissions for private projects
111
            if ($private && $model === 'projectPermissionModel') {
112
                continue;
113
            }
114
115
            if (!$this->$model->duplicate($src_project_id, $dst_project_id)) {
116
                $this->db->cancelTransaction();
0 ignored issues
show
Documentation introduced by
The property db does not exist on object<Jitamin\Model\ProjectDuplicationModel>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
117
118
                return false;
119
            }
120
        }
121
122
        if (!$this->makeOwnerManager($dst_project_id, $owner_id)) {
123
            $this->db->cancelTransaction();
0 ignored issues
show
Documentation introduced by
The property db does not exist on object<Jitamin\Model\ProjectDuplicationModel>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
124
125
            return false;
126
        }
127
128
        $this->db->closeTransaction();
0 ignored issues
show
Documentation introduced by
The property db does not exist on object<Jitamin\Model\ProjectDuplicationModel>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
129
130
        return (int) $dst_project_id;
131
    }
132
133
    /**
134
     * Create a project from another one.
135
     *
136
     * @param int    $src_project_id
137
     * @param int    $owner_id
138
     * @param string $name
139
     * @param bool   $private
140
     *
141
     * @return int
142
     */
143
    private function copy($src_project_id, $owner_id = 0, $name = null, $private = null)
144
    {
145
        $project = $this->projectModel->getById($src_project_id);
0 ignored issues
show
Documentation introduced by
The property projectModel does not exist on object<Jitamin\Model\ProjectDuplicationModel>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
146
        $is_private = empty($project['is_private']) ? 0 : 1;
147
148
        $values = [
149
            'name'             => $name ?: $this->getClonedProjectName($project['name']),
150
            'is_active'        => 1,
151
            'last_modified'    => time(),
152
            'token'            => '',
153
            'is_public'        => 0,
154
            'is_private'       => $private ? 1 : $is_private,
155
            'owner_id'         => $owner_id,
156
            'priority_default' => $project['priority_default'],
157
            'priority_start'   => $project['priority_start'],
158
            'priority_end'     => $project['priority_end'],
159
        ];
160
161
        if (!$this->db->table(ProjectModel::TABLE)->save($values)) {
0 ignored issues
show
Documentation introduced by
The property db does not exist on object<Jitamin\Model\ProjectDuplicationModel>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
162
            return false;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return false; (false) is incompatible with the return type documented by Jitamin\Model\ProjectDuplicationModel::copy of type integer.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
163
        }
164
165
        return $this->db->getLastId();
0 ignored issues
show
Documentation introduced by
The property db does not exist on object<Jitamin\Model\ProjectDuplicationModel>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
166
    }
167
168
    /**
169
     * Make sure that the creator of the duplicated project is also owner.
170
     *
171
     * @param int $dst_project_id
172
     * @param int $owner_id
173
     *
174
     * @return bool
175
     */
176
    private function makeOwnerManager($dst_project_id, $owner_id)
177
    {
178
        if ($owner_id > 0) {
179
            $this->projectUserRoleModel->removeUser($dst_project_id, $owner_id);
0 ignored issues
show
Documentation introduced by
The property projectUserRoleModel does not exist on object<Jitamin\Model\ProjectDuplicationModel>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
180
181
            if (!$this->projectUserRoleModel->addUser($dst_project_id, $owner_id, Role::PROJECT_MANAGER)) {
0 ignored issues
show
Documentation introduced by
The property projectUserRoleModel does not exist on object<Jitamin\Model\ProjectDuplicationModel>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
182
                return false;
183
            }
184
        }
185
186
        return true;
187
    }
188
}
189