Completed
Push — master ( 0c59cf...e9fd90 )
by Arthur
04:31
created

Credit::getBalanceSign()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 4
rs 10
cc 2
eloc 2
nc 2
nop 0
1
<?php namespace BB\Services;
2
3
use BB\Entities\User;
4
use BB\Exceptions\InvalidDataException;
5
use BB\Exceptions\NotImplementedException;
6
use BB\Repo\PaymentRepository;
7
use BB\Repo\UserRepository;
8
9
class Credit
10
{
11
12
    private $userId;
13
    /**
14
     * @var PaymentRepository
15
     */
16
    private $paymentRepository;
17
    /**
18
     * @var UserRepository
19
     */
20
    private $userRepository;
21
    private $user;
22
23
    /**
24
     * @param PaymentRepository $paymentRepository
25
     * @param UserRepository    $userRepository
26
     */
27
    public function __construct(PaymentRepository $paymentRepository, UserRepository $userRepository)
28
    {
29
        $this->paymentRepository = $paymentRepository;
30
        $this->userRepository = $userRepository;
31
    }
32
33
34
    /**
35
     * @param mixed $userId
36
     */
37
    public function setUserId($userId)
38
    {
39
        $this->userId = $userId;
40
        $this->user = $this->userRepository->getById($this->userId);
41
    }
42
43
44
    public function recalculate()
45
    {
46
        if (! $this->user instanceof User) {
47
            throw new InvalidDataException("User not set");
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal User not set does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
48
        }
49
        $runningTotal = 0;
50
        $positivePayments = $this->paymentRepository->getUserPaymentsByReason($this->userId, 'balance');
51
        $negativePayments = $this->paymentRepository->getUserPaymentsBySource($this->userId, 'balance');
52
53
        foreach ($positivePayments as $payment) {
54
            $runningTotal = $runningTotal + ($payment->amount * 100);
55
        }
56
        foreach ($negativePayments as $payment) {
57
            $runningTotal = $runningTotal - ($payment->amount * 100);
58
        }
59
        $this->user->cash_balance = $runningTotal;
0 ignored issues
show
Documentation introduced by
The property cash_balance does not exist on object<BB\Entities\User>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write 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.");
        }
    }

}

Since the property has write access only, you can use the @property-write 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...
60
        $this->user->save();
61
    }
62
63
    /**
64
     * Can the user spend money they don't have and if so how much?
65
     * @param $reason
66
     * @return int
67
     */
68 View Code Duplication
    public function acceptableNegativeBalance($reason)
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...
69
    {
70
        switch ($reason) {
71
            case 'storage-box':
72
                return 0;
73
            case 'subscription':
74
                return 0;
75
            case 'induction':
76
                return 0;
77
            case 'equipment-fee':
78
                return 5;
79
            default:
80
                return 0;
81
        }
82
    }
83
84
    /**
85
     * Get the users balance
86
     * @return float
87
     */
88
    public function getBalance()
89
    {
90
        return $this->user->cash_balance / 100;
91
    }
92
93
    /**
94
     * Get the users balance sign (positive/negative)
95
     * @return string
96
     */
97
    public function getBalanceSign()
98
    {
99
        return ( (int) $this->getBalance() >= 0 ? 'positive' : 'negative' );
100
    }
101
102
    public function getBalanceFormatted()
103
    {
104
        return '&pound;' . number_format(($this->user->cash_balance / 100), 2);
105
    }
106
107
    public function getBalancePaymentsPaginated()
108
    {
109
        return $this->paymentRepository->getBalancePaymentsPaginated($this->user->id);
110
    }
111
112
113
}