TaskRecurrenceModel::duplicateRecurringTask()   B
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 30
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 18
nc 4
nop 1
dl 0
loc 30
rs 8.5806
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 DateInterval;
15
use DateTime;
16
17
/**
18
 * Task Recurrence.
19
 */
20
class TaskRecurrenceModel extends TaskDuplicationModel
21
{
22
    /**
23
     * Return the list user selectable recurrence status.
24
     *
25
     * @return array
26
     */
27
    public function getRecurrenceStatusList()
28
    {
29
        return [
30
            TaskModel::RECURRING_STATUS_NONE    => t('No'),
31
            TaskModel::RECURRING_STATUS_PENDING => t('Yes'),
32
        ];
33
    }
34
35
    /**
36
     * Return the list recurrence triggers.
37
     *
38
     * @return array
39
     */
40 View Code Duplication
    public function getRecurrenceTriggerList()
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...
41
    {
42
        return [
43
            TaskModel::RECURRING_TRIGGER_FIRST_COLUMN => t('When task is moved from first column'),
44
            TaskModel::RECURRING_TRIGGER_LAST_COLUMN  => t('When task is moved to last column'),
45
            TaskModel::RECURRING_TRIGGER_CLOSE        => t('When task is closed'),
46
        ];
47
    }
48
49
    /**
50
     * Return the list options to calculate recurrence due date.
51
     *
52
     * @return array
53
     */
54
    public function getRecurrenceBasedateList()
55
    {
56
        return [
57
            TaskModel::RECURRING_BASEDATE_DUEDATE     => t('Existing due date'),
58
            TaskModel::RECURRING_BASEDATE_TRIGGERDATE => t('Action date'),
59
        ];
60
    }
61
62
    /**
63
     * Return the list recurrence timeframes.
64
     *
65
     * @return array
66
     */
67 View Code Duplication
    public function getRecurrenceTimeframeList()
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...
68
    {
69
        return [
70
            TaskModel::RECURRING_TIMEFRAME_DAYS   => t('Day(s)'),
71
            TaskModel::RECURRING_TIMEFRAME_MONTHS => t('Month(s)'),
72
            TaskModel::RECURRING_TIMEFRAME_YEARS  => t('Year(s)'),
73
        ];
74
    }
75
76
    /**
77
     * Duplicate recurring task.
78
     *
79
     * @param int $task_id Task id
80
     *
81
     * @return bool|int Recurrence task id
82
     */
83
    public function duplicateRecurringTask($task_id)
84
    {
85
        $values = $this->copyFields($task_id);
86
87
        if ($values['recurrence_status'] == TaskModel::RECURRING_STATUS_PENDING) {
88
            $values['recurrence_parent'] = $task_id;
89
            $values['column_id'] = $this->columnModel->getFirstColumnId($values['project_id']);
0 ignored issues
show
Documentation introduced by
The property columnModel does not exist on object<Jitamin\Model\TaskRecurrenceModel>. 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...
90
            $this->calculateRecurringTaskDueDate($values);
91
92
            $recurring_task_id = $this->save($task_id, $values);
93
94
            if ($recurring_task_id !== false) {
95
                $this->tagDuplicationModel->duplicateTaskTags($task_id, $recurring_task_id);
0 ignored issues
show
Documentation introduced by
The property tagDuplicationModel does not exist on object<Jitamin\Model\TaskRecurrenceModel>. 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...
96
97
                $parent_update = $this->db
0 ignored issues
show
Documentation introduced by
The property db does not exist on object<Jitamin\Model\TaskRecurrenceModel>. 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
                    ->table(TaskModel::TABLE)
99
                    ->eq('id', $task_id)
100
                    ->update([
101
                        'recurrence_status' => TaskModel::RECURRING_STATUS_PROCESSED,
102
                        'recurrence_child'  => $recurring_task_id,
103
                    ]);
104
105
                if ($parent_update) {
106
                    return $recurring_task_id;
107
                }
108
            }
109
        }
110
111
        return false;
112
    }
113
114
    /**
115
     * Calculate new due date for new recurrence task.
116
     *
117
     * @param array $values Task fields
118
     */
119
    public function calculateRecurringTaskDueDate(array &$values)
120
    {
121
        if (!empty($values['date_due']) && $values['recurrence_factor'] != 0) {
122
            if ($values['recurrence_basedate'] == TaskModel::RECURRING_BASEDATE_TRIGGERDATE) {
123
                $values['date_due'] = time();
124
            }
125
126
            $factor = abs($values['recurrence_factor']);
127
            $subtract = $values['recurrence_factor'] < 0;
128
129
            switch ($values['recurrence_timeframe']) {
130
                case TaskModel::RECURRING_TIMEFRAME_MONTHS:
131
                    $interval = 'P'.$factor.'M';
132
                    break;
133
                case TaskModel::RECURRING_TIMEFRAME_YEARS:
134
                    $interval = 'P'.$factor.'Y';
135
                    break;
136
                default:
137
                    $interval = 'P'.$factor.'D';
138
            }
139
140
            $date_due = new DateTime();
141
            $date_due->setTimestamp($values['date_due']);
142
143
            $subtract ? $date_due->sub(new DateInterval($interval)) : $date_due->add(new DateInterval($interval));
144
145
            $values['date_due'] = $date_due->getTimestamp();
146
        }
147
    }
148
}
149