Passed
Pull Request — master (#248)
by Kevin
08:12 queued 04:32
created

NoBeanFoundException::missPrimaryKeyRecord()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 8
c 0
b 0
f 0
dl 0
loc 11
rs 10
cc 1
nc 1
nop 4
1
<?php
2
declare(strict_types=1);
3
4
namespace TheCodingMachine\TDBM;
5
6
use Exception;
7
8
/**
9
 * An exception thrown if no rows are returned when TDBMService->findObjectOrFail is called.
10
 */
11
class NoBeanFoundException extends TDBMException
12
{
13
14
    /**
15
     * @var string
16
     */
17
    private $tableName;
18
19
    /**
20
     * @var string
21
     */
22
    private $className;
23
24
    /**
25
     * @var string[]
26
     */
27
    private $primaryKeys;
28
29
    /**
30
     * @param string $tableName
31
     * @param string[] $primaryKeys
32
     * @param string $className
33
     * @param Exception $previous
34
     * @return NoBeanFoundException
35
     */
36
    public static function missPrimaryKeyRecord(string $tableName, array $primaryKeys, string $className, Exception $previous) : self
37
    {
38
        $primaryKeysStringified = implode(' and ', array_map(function ($key, $value) {
39
            return "'".$key."' = ".$value;
40
        }, array_keys($primaryKeys), $primaryKeys));
41
        $exception = new self("No result found for query on table '".$tableName."' for ".$primaryKeysStringified, 0, $previous);
42
        $exception->tableName = $tableName;
43
        $exception->primaryKeys = $primaryKeys;
44
        $exception->className = $className;
45
46
        return $exception;
47
    }
48
49
    public static function missFilterRecord(string $tableName) : self
50
    {
51
        return new self("No result found for query on table '".$tableName."'");
52
    }
53
54
    /**
55
     * @return string
56
     */
57
    public function getTableName(): string
58
    {
59
        return $this->tableName;
60
    }
61
62
    /**
63
     * @return string
64
     */
65
    public function getClassName(): string
66
    {
67
        return $this->className;
68
    }
69
70
    /**
71
     * @return string[]
72
     */
73
    public function getPrimaryKeys(): array
74
    {
75
        return $this->primaryKeys;
76
    }
77
}
78