Completed
Push — master ( d62670...fa256e )
by Andrii
02:10
created

BillRepository::save()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 25
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 25
ccs 0
cts 25
cp 0
rs 8.8571
c 0
b 0
f 0
cc 2
eloc 21
nc 2
nop 1
crap 6
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\repositories;
12
13
use DateTime;
14
use hiqdev\yii\DataMapper\expressions\CallExpression;
15
use hiqdev\yii\DataMapper\expressions\HstoreExpression;
16
use hiqdev\yii\DataMapper\components\EntityManagerInterface;
17
use hiqdev\php\billing\bill\BillInterface;
18
use hiqdev\php\billing\bill\BillFactoryInterface;
19
use hiqdev\php\billing\customer\Customer;
20
use hiqdev\php\billing\target\Target;
21
use hiqdev\php\billing\type\Type;
22
use hiqdev\php\units\Quantity;
23
use Money\Currency;
24
use Money\Money;
25
26
class BillRepository extends \hiapi\repositories\BaseRepository
27
{
28
    /**
29
     * @var BillFactoryInterface
30
     */
31
    protected $factory;
32
33
    public function __construct(
34
        EntityManagerInterface $em,
35
        BillFactoryInterface $factory,
36
        array $config = []
37
    ) {
38
        parent::__construct($config);
39
40
        $this->em = $em;
0 ignored issues
show
Documentation introduced by
The property em does not exist on object<hiqdev\billing\hi...itories\BillRepository>. 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...
41
        $this->factory = $factory;
42
    }
43
44
    public function create(array $row)
45
    {
46
        $row['type'] = $this->createEntity(Type::class, $row['type']);
47
        $row['time'] = new DateTime($row['time']);
48
        $row['quantity'] = Quantity::create('megabyte', $row['quantity']['quantity']);
49
        $currency = new Currency(strtoupper($row['sum']['currency']));
50
        $row['sum'] = new Money($row['sum']['amount'], $currency);
51
        $row['customer'] = $this->createEntity(Customer::class, $row['customer']);
52
        $row['target'] = $this->createEntity(Target::class, $row['target']);
53
54
        return parent::create($row);
55
    }
56
57
    public function save(BillInterface $bill)
58
    {
59
        $chargeIds = [];
60
        foreach ($bill->getCharges() as $charge) {
61
            $this->em->save($charge);
0 ignored issues
show
Documentation introduced by
The property em does not exist on object<hiqdev\billing\hi...itories\BillRepository>. 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...
62
            $chargeIds[] = $charge->getId();
63
        }
64
        $hstore = new HstoreExpression([
65
            'id'            => $bill->getId(),
66
            'object_id'     => $bill->getTarget()->getId(),
67
            'tariff_id'     => $bill->getPlan()->getId(),
68
            'type_id'       => $bill->getType()->getId(),
69
            'type'          => $bill->getType()->getName(),
70
            'buyer_id'      => $bill->getCustomer()->getId(),
71
            'buyer'         => $bill->getCustomer()->getLogin(),
72
            'currency'      => $bill->getSum()->getCurrency()->getCode(),
73
            'sum'           => $bill->getSum()->getAmount(),
74
            'quantity'      => $bill->getQuantity()->getQuantity(),
75
            'is_finished'   => $bill->getIsFinished(),
76
            'charge_ids'    => implode(',', $chargeIds),
77
        ]);
78
        $call = new CallExpression('set_bill', [$hstore]);
79
        $command = $this->em->getConnection()->createSelect($call);
0 ignored issues
show
Documentation introduced by
The property em does not exist on object<hiqdev\billing\hi...itories\BillRepository>. 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...
80
        $bill->setId($command->scalar());
81
    }
82
}
83