Completed
Push — master ( ffc0ef...053644 )
by Nasrul Hazim
01:54
created

PayslipProcessor   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 25
dl 0
loc 50
rs 10
c 0
b 0
f 0
wmc 9

4 Methods

Rating   Name   Duplication   Size   Complexity  
A make() 0 3 1
A __construct() 0 13 5
A payslip() 0 5 1
A calculate() 0 17 2
1
<?php
2
3
namespace App\Processors;
4
5
use App\Contracts\CalculateContract;
0 ignored issues
show
Bug introduced by
The type App\Contracts\CalculateContract was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use App\Models\Payslip;
0 ignored issues
show
Bug introduced by
The type App\Models\Payslip was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
8
class PayslipProcessor implements CalculateContract
9
{
10
    public $payslip;
11
12
    public function __construct($identifier = null)
13
    {
14
        if (! is_null($identifier)) {
15
            if (is_string($identifier) || is_int($identifier)) {
16
                $this->payslip = Payslip::query()
17
                    ->with('earnings', 'deductions', 'payroll', 'employee', 'employee.salary')
18
                    ->whereId($identifier)
19
                    ->orWhere('hashslug', $identifier)
20
                    ->firstOrFail();
21
            }
22
23
            if (is_object($identifier)) {
24
                $this->payslip($identifier);
25
            }
26
        }
27
    }
28
29
    public static function make($identifier = null)
30
    {
31
        return new self($identifier);
32
    }
33
34
    public function payslip(Payslip $payslip)
35
    {
36
        $this->payslip = $payslip;
37
38
        return $this;
39
    }
40
41
    public function calculate()
42
    {
43
        if ($this->payslip) {
44
            $employee   = $this->payslip->employee;
45
            $salary     = $employee->salary;
46
            $payroll    = $this->payslip->payroll;
0 ignored issues
show
Unused Code introduced by
The assignment to $payroll is dead and can be removed.
Loading history...
47
            $earnings   = $this->payslip->earnings;
48
            $deductions = $this->payslip->deductions;
49
50
            $this->payslip->basic_salary = $salary->amount;
51
            $this->payslip->gross_salary = $gross_salary = ($earnings->sum('amount') + $salary->amount);
52
            $this->payslip->net_salary   = $gross_salary - $deductions->sum('amount');
53
54
            $this->payslip->save();
55
        }
56
57
        return $this;
58
    }
59
}
60