TaskImport::prepare()   F
last analyzed

Complexity

Conditions 9
Paths 256

Size

Total Lines 47
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 28
nc 256
nop 1
dl 0
loc 47
rs 3.3333
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\Import;
13
14
use Jitamin\Foundation\Base;
15
use Jitamin\Foundation\Csv;
16
use SimpleValidator\Validator;
17
use SimpleValidator\Validators;
18
19
/**
20
 * Task Import.
21
 */
22
class TaskImport extends Base
23
{
24
    /**
25
     * Number of successful import.
26
     *
27
     * @var int
28
     */
29
    public $counter = 0;
30
31
    /**
32
     * Project id to import tasks.
33
     *
34
     * @var int
35
     */
36
    public $projectId;
37
38
    /**
39
     * Get mapping between CSV header and SQL columns.
40
     *
41
     * @return array
42
     */
43
    public function getColumnMapping()
44
    {
45
        return [
46
            'reference'      => 'Reference',
47
            'title'          => 'Title',
48
            'description'    => 'Description',
49
            'assignee'       => 'Assignee Username',
50
            'creator'        => 'Creator Username',
51
            'color'          => 'Color Name',
52
            'column'         => 'Column Name',
53
            'category'       => 'Category Name',
54
            'swimlane'       => 'Swimlane Name',
55
            'score'          => 'Complexity',
56
            'time_estimated' => 'Time Estimated',
57
            'time_spent'     => 'Time Spent',
58
            'date_due'       => 'Due Date',
59
            'is_active'      => 'Closed',
60
        ];
61
    }
62
63
    /**
64
     * Import a single row.
65
     *
66
     * @param array $row
67
     * @param int   $line_number
68
     */
69 View Code Duplication
    public function import(array $row, $line_number)
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...
70
    {
71
        $row = $this->prepare($row);
72
73
        if ($this->validateCreation($row)) {
74
            if ($this->taskModel->create($row) > 0) {
0 ignored issues
show
Documentation introduced by
The property taskModel does not exist on object<Jitamin\Import\TaskImport>. 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...
75
                $this->logger->debug('TaskImport: imported successfully line '.$line_number);
0 ignored issues
show
Documentation introduced by
The property logger does not exist on object<Jitamin\Import\TaskImport>. 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...
76
                $this->counter++;
77
            } else {
78
                $this->logger->error('TaskImport: creation error at line '.$line_number);
0 ignored issues
show
Documentation introduced by
The property logger does not exist on object<Jitamin\Import\TaskImport>. 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...
79
            }
80
        } else {
81
            $this->logger->error('TaskImport: validation error at line '.$line_number);
0 ignored issues
show
Documentation introduced by
The property logger does not exist on object<Jitamin\Import\TaskImport>. 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...
82
        }
83
    }
84
85
    /**
86
     * Format row before validation.
87
     *
88
     * @param array $row
89
     *
90
     * @return array
91
     */
92
    public function prepare(array $row)
93
    {
94
        $values = [];
95
        $values['project_id'] = $this->projectId;
96
        $values['reference'] = $row['reference'];
97
        $values['title'] = $row['title'];
98
        $values['description'] = $row['description'];
99
        $values['is_active'] = Csv::getBooleanValue($row['is_active']) == 1 ? 0 : 1;
100
        $values['score'] = (int) $row['score'];
101
        $values['time_estimated'] = (float) $row['time_estimated'];
102
        $values['time_spent'] = (float) $row['time_spent'];
103
104
        if (!empty($row['assignee'])) {
105
            $values['owner_id'] = $this->userModel->getIdByUsername($row['assignee']);
0 ignored issues
show
Documentation introduced by
The property userModel does not exist on object<Jitamin\Import\TaskImport>. 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...
106
        }
107
108
        if (!empty($row['creator'])) {
109
            $values['creator_id'] = $this->userModel->getIdByUsername($row['creator']);
0 ignored issues
show
Documentation introduced by
The property userModel does not exist on object<Jitamin\Import\TaskImport>. 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...
110
        }
111
112
        if (!empty($row['color'])) {
113
            $values['color_id'] = $this->colorModel->find($row['color']);
0 ignored issues
show
Documentation introduced by
The property colorModel does not exist on object<Jitamin\Import\TaskImport>. 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...
114
        }
115
116
        if (!empty($row['column'])) {
117
            $values['column_id'] = $this->columnModel->getColumnIdByTitle($this->projectId, $row['column']);
0 ignored issues
show
Documentation introduced by
The property columnModel does not exist on object<Jitamin\Import\TaskImport>. 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...
118
        }
119
120
        if (!empty($row['category'])) {
121
            $values['category_id'] = $this->categoryModel->getIdByName($this->projectId, $row['category']);
0 ignored issues
show
Documentation introduced by
The property categoryModel does not exist on object<Jitamin\Import\TaskImport>. 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...
122
        }
123
124
        if (!empty($row['swimlane'])) {
125
            $values['swimlane_id'] = $this->swimlaneModel->getIdByName($this->projectId, $row['swimlane']);
0 ignored issues
show
Documentation introduced by
The property swimlaneModel does not exist on object<Jitamin\Import\TaskImport>. 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...
126
        }
127
128
        if (!empty($row['date_due'])) {
129
            $values['date_due'] = $this->dateParser->getTimestampFromIsoFormat($row['date_due']);
0 ignored issues
show
Documentation introduced by
The property dateParser does not exist on object<Jitamin\Import\TaskImport>. 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...
130
        }
131
132
        $this->helper->model->removeEmptyFields(
0 ignored issues
show
Documentation introduced by
The property helper does not exist on object<Jitamin\Import\TaskImport>. 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...
133
            $values,
134
            ['owner_id', 'creator_id', 'color_id', 'column_id', 'category_id', 'swimlane_id', 'date_due']
135
        );
136
137
        return $values;
138
    }
139
140
    /**
141
     * Validate user creation.
142
     *
143
     * @param array $values
144
     *
145
     * @return bool
146
     */
147 View Code Duplication
    public function validateCreation(array $values)
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...
148
    {
149
        $v = new Validator($values, [
150
            new Validators\Integer('project_id', t('This value must be an integer')),
151
            new Validators\Required('project_id', t('The project is required')),
152
            new Validators\Required('title', t('The title is required')),
153
            new Validators\MaxLength('title', t('The maximum length is %d characters', 200), 200),
154
            new Validators\MaxLength('reference', t('The maximum length is %d characters', 50), 50),
155
        ]);
156
157
        return $v->execute();
158
    }
159
}
160