Completed
Push — master ( b81036...d62670 )
by Andrii
05:59
created

ChargeRepository::save()   B

Complexity

Conditions 2
Paths 1

Size

Total Lines 25
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 25
ccs 0
cts 24
cp 0
rs 8.8571
c 0
b 0
f 0
cc 2
eloc 22
nc 1
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\charge;
12
13
use hiapi\db\CallExpression;
14
use hiapi\db\HstoreExpression;
15
use hiapi\components\ConnectionInterface;
16
use hiapi\components\EntityManagerInterface;
17
use hiapi\query\Specification;
18
use hiapi\repositories\BaseRepository;
19
use hiqdev\php\billing\charge\Charge;
20
use hiqdev\php\billing\charge\ChargeFactoryInterface;
21
use hiqdev\php\billing\sale\Sale;
22
23
class ChargeRepository extends BaseRepository
24
{
25
    public $queryClass = ChargeQuery::class;
26
27
    /**
28
     * @var ChargeFactory
29
     */
30
    protected $factory;
31
32
    public function __construct(
33
        ConnectionInterface $db,
34
        EntityManagerInterface $em,
35
        ChargeFactoryInterface $factory,
36
        array $config = []
37
    ) {
38
        parent::__construct($config);
39
40
        $this->db = $db;
41
        $this->em = $em;
0 ignored issues
show
Documentation introduced by
The property em does not exist on object<hiqdev\billing\hi...harge\ChargeRepository>. 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...
42
        $this->factory = $factory;
0 ignored issues
show
Documentation Bug introduced by
It seems like $factory of type object<hiqdev\php\billin...ChargeFactoryInterface> is incompatible with the declared type object<hiqdev\billing\hiapi\charge\ChargeFactory> of property $factory.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
43
    }
44
45
    public function save(Charge $charge)
46
    {
47
        $action = $charge->getAction();
48
        $sale = new Sale(null, $action->getTarget(), $action->getCustomer(), $charge->getPrice()->getPlan());
49
        $action->setSale($sale);
50
        $this->em->save($action);
0 ignored issues
show
Documentation introduced by
The property em does not exist on object<hiqdev\billing\hi...harge\ChargeRepository>. 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...
51
        $hstore = new HstoreExpression(array_filter([
52
            'id'        => $charge->getId(),
53
            'object_id' => $charge->getTarget()->getId(),
54
            'tariff_id' => $sale->getPlan()->getId(),
55
            'action_id' => $action->getId(),
56
            'type_id'   => $action->getType()->getId(),
57
            'type'      => $action->getType()->getName(),
58
            'buyer_id'  => $action->getCustomer()->getId(),
59
            'buyer'     => $action->getCustomer()->getLogin(),
60
            'currency'  => $charge->getSum()->getCurrency()->getCode(),
61
            'sum'       => $charge->getSum()->getAmount(),
62
            'quantity'  => $charge->getUsage()->getQuantity(),
63
            'sale_id'   => $sale ? $this->em->findId($sale) : null,
0 ignored issues
show
Documentation introduced by
The property em does not exist on object<hiqdev\billing\hi...harge\ChargeRepository>. 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...
64
            'time'      => $charge->getTime(),
65
        ]));
66
        $call = new CallExpression('set_charge', [$hstore]);
67
        $command = $this->em->getConnection()->createSelect($call);
0 ignored issues
show
Documentation introduced by
The property em does not exist on object<hiqdev\billing\hi...harge\ChargeRepository>. 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...
68
        $charge->setId($command->scalar());
69
    }
70
}
71