HasTransactionsTrait::addTransaction()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 0
cts 8
cp 0
rs 9.9
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 6
1
<?php
2
3
namespace ByTIC\GoogleAnalytics\Tracking\Data\Tracker;
4
5
use ByTIC\GoogleAnalytics\Tracking\Data\Ecommerce\Transaction;
6
use InvalidArgumentException;
7
8
/**
9
 * Trait HasTransactionsTrait
10
 * @package ByTIC\GoogleAnalytics\Tracking\Data\Tracker
11
 */
12
trait HasTransactionsTrait
13
{
14
    /**
15
     * @var Transaction[]
16
     */
17
    protected $transactions = [];
18
19
    /**
20
     * @return Transaction[]
21
     */
22
    public function getTransactions()
23
    {
24
        return $this->transactions;
25
    }
26
27
    /**
28
     * @param Transaction $transaction
29
     */
30
    public function addTransaction(Transaction $transaction)
31
    {
32
        $id = $transaction->getId();
33
        if (array_key_exists($id, $this->transactions)) {
34
            throw new InvalidArgumentException(sprintf(
35
                'Cannot add transaction with id %s, it already exists',
36
                $id
37
            ));
38
        }
39
        $this->transactions[$id] = $transaction;
40
    }
41
}
42