Completed
Push — master ( 22330a...e8d007 )
by Dmitry
01:58
created

PriceRepository::negotiatePriceAndUnit()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 0
cts 9
cp 0
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 3
nop 3
crap 12
1
<?php
2
/**
3
 * API for Billing
4
 *
5
 * @link      https://github.com/hiqdev/billing-hiapi
6
 * @package   billing-hiapi
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2017, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hiqdev\billing\hiapi\plan;
12
13
use hiqdev\yii\DataMapper\components\ConnectionInterface;
14
use hiqdev\php\billing\price\PriceFactoryInterface;
15
use hiqdev\php\billing\target\Target;
16
use hiqdev\php\billing\type\Type;
17
use hiqdev\php\units\Unit;
18
use hiqdev\php\units\Quantity;
19
use hiqdev\yii2\collection\Model;
20
use Money\Currency;
21
use Money\Money;
22
use Money\Number;
23
use yii\helpers\Json;
24
25
class PriceRepository extends \hiqdev\yii\DataMapper\repositories\BaseRepository
26
{
27
    public $queryClass = PriceQuery::class;
28
29
    /**
30
     * @var PriceFactoryInterface
31
     */
32
    protected $factory;
33
34
    public function __construct(
35
        ConnectionInterface $db,
36
        PriceFactoryInterface $factory,
37
        array $config = []
38
    ) {
39
        parent::__construct($config);
40
41
        $this->db = $db;
42
        $this->factory = $factory;
43
    }
44
45
    public function create(array $row)
46
    {
47
        $row['target'] = $this->createEntity(Target::class, $row['target']);
48
        $row['type'] = $this->createEntity(Type::class, $row['type']);
49
        $row['unit'] = Unit::create($row['prepaid']['unit']);
50
        $row['prepaid'] = Quantity::create($row['unit'], $row['prepaid']['quantity']);
51
        $row['currency'] = new Currency(strtoupper($row['price']['currency']));
52
        $row['price'] = $this->negotiatePriceAndUnit($row['price']['amount'], $row['unit'], $row['currency']);
53
        $data = Json::decode($row['data']);
54
        $row['sums'] = empty($data['sums']) ? [] : $data['sums'];
55
56
        return parent::create($row);
57
    }
58
59
    /**
60
     * @param string $amount
61
     * @param Unit $unit
62
     * @param Currency $currency
63
     * @return Money
64
     */
65
    protected function negotiatePriceAndUnit($amount, Unit &$unit, Currency $currency)
0 ignored issues
show
Unused Code introduced by
The parameter $unit is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
66
    {
67
        if (filter_var($amount, FILTER_VALIDATE_INT) === false) {
68
            $numberFromString = Number::fromString($amount);
69
            if (!$numberFromString->isInteger()) {
70
                // $smaller = $unit->getSmaller();
0 ignored issues
show
Unused Code Comprehensibility introduced by
55% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
71
                // $amount = $amount/$smaller->getFactor();
0 ignored issues
show
Unused Code Comprehensibility introduced by
54% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
72
                // TODO: !!!
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
73
                return $this->negotiatePriceAndUnit($amount, $smaller, $currency);
74
            }
75
        }
76
77
        return new Money($amount, $currency);
78
    }
79
80
}
81