Completed
Push — api/develop ( d18cde...f05f55 )
by Bertrand
08:06
created

EmployeeController::show()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 4
nc 1
nop 1
crap 1
1
<?php
2
3
/**
4
 * This file is part of the HRis Software package.
5
 *
6
 * HRis - Human Resource and Payroll System
7
 *
8
 * @link http://github.com/HB-Co/HRis
9
 */
10
namespace HRis\Api\Controllers;
11
12
use HRis\Api\Eloquent\CustomField;
13
use HRis\Api\Eloquent\CustomFieldValue;
14
use HRis\Api\Eloquent\Employee;
15
use HRis\Api\Requests\EmployeeRequest;
16
use HRis\Api\Transformers\EmployeeTransformer;
17
18
class EmployeeController extends BaseController
19
{
20
    /**
21
     * @var CustomFieldValue
22
     */
23
    protected $custom_field_value;
24
25
    /**
26
     * @var Employee
27
     */
28
    protected $employee;
29
30
    /**
31
     * @var CustomField
32
     */
33
    protected $custom_field;
34
35
    /**
36
     * @param Employee         $employee
37
     * @param CustomField      $custom_field
38
     * @param CustomFieldValue $custom_field_value
39
     *
40
     * @author Bertrand Kintanar <[email protected]>
41
     */
42 6
    public function __construct(Employee $employee, CustomField $custom_field, CustomFieldValue $custom_field_value)
43
    {
44 6
        $this->employee = $employee;
45 6
        $this->custom_field = $custom_field;
46 6
        $this->custom_field_value = $custom_field_value;
47 6
    }
48
49
    /**
50
     * @param Employee $employee
51
     *
52
     * @author Bertrand Kintanar <[email protected]>
53
     *
54
     * @return \Dingo\Api\Http\Response
55
     */
56 6
    public function show(Employee $employee)
57
    {
58 6
        $this->initializeCustomFieldValues($employee->employee_id);
0 ignored issues
show
Documentation introduced by
The property employee_id does not exist on object<HRis\Api\Eloquent\Employee>. 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...
59
60 6
        $employee = $this->employee->getEmployeeById($employee->employee_id, null);
0 ignored issues
show
Documentation introduced by
The property employee_id does not exist on object<HRis\Api\Eloquent\Employee>. 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...
61
62 6
        return $this->item($employee, new EmployeeTransformer());
0 ignored issues
show
Documentation Bug introduced by
The method item does not exist on object<HRis\Api\Controllers\EmployeeController>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
63
    }
64
65
    /**
66
     * @param $employee_id
67
     *
68
     * @author Bertrand Kintanar <[email protected]>
69
     */
70 6
    private function initializeCustomFieldValues($employee_id)
71
    {
72 6
        $employee_id = $this->employee->whereEmployeeId($employee_id)->value('id');
0 ignored issues
show
Documentation Bug introduced by
The method whereEmployeeId does not exist on object<HRis\Api\Eloquent\Employee>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
73 6
        $custom_fields = $this->custom_field->get();
0 ignored issues
show
Documentation Bug introduced by
The method get does not exist on object<HRis\Api\Eloquent\CustomField>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
74
75 6
        foreach ($custom_fields as $custom_field) {
76 6
            if (!$this->custom_field_value->whereCustomFieldId($custom_field->id)->whereEmployeeId($employee_id)->first()) {
0 ignored issues
show
Bug introduced by
The method whereCustomFieldId() does not exist on HRis\Api\Eloquent\CustomFieldValue. Did you maybe mean customField()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
77
                $data = [
78 6
                    'custom_field_id' => $custom_field->id,
79 6
                    'employee_id'     => $employee_id,
80 6
                    'value'           => null,
81 6
                ];
82
83 6
                $custom_field_value = $this->custom_field_value->firstOrCreate($data);
84 6
                $custom_field_value->save();
85 6
            }
86 6
        }
87 6
    }
88
}
89