Passed
Push — master ( 6bcc6d...405e10 )
by F
03:06
created

Address::findById()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 9
rs 10
1
<?php
2
3
/*
4
 * PWWeb\Localisation\Models\Address Model
5
 *
6
 * Standard Address Model.
7
 *
8
 * @package   PWWeb\Localisation
9
 * @author    Frank Pillukeit <[email protected]>
10
 * @copyright 2020 pw-websolutions.com
11
 * @license   http://www.opensource.org/licenses/mit-license.html  MIT License
12
 */
13
14
namespace PWWeb\Localisation\Models;
15
16
use Illuminate\Database\Eloquent\Collection;
17
use Illuminate\Database\Eloquent\Model;
18
use PWWeb\Localisation\Contracts\Address as AddressContract;
19
use PWWeb\Localisation\LocalisationRegistrar;
20
use PWWeb\Localisation\Models\Address\Type;
0 ignored issues
show
Bug introduced by
The type PWWeb\Localisation\Models\Address\Type was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
21
22
class Address extends Model implements AddressContract
23
{
24
    /**
25
     * The attributes that are mass assignable.
26
     *
27
     * @var array
28
     */
29
    protected $fillable = [
30
        'street', 'street2', 'city', 'state', 'postcode', 'country_id', 'type_id', 'lat', 'lng',
31
    ];
32
33
    /**
34
     * Constructor.
35
     *
36
     * @param array $attributes additional attributes for model initialisation
37
     */
38
    public function __construct(array $attributes = [])
39
    {
40
        parent::__construct($attributes);
41
42
        $this->setTable(config('localisation.table_names.addresses'));
43
    }
44
45
    public function type()
46
    {
47
        return $this->belongsTo(Address\Type::class);
48
    }
49
50
    public function country()
51
    {
52
        return $this->belongsTo(Country::class);
53
    }
54
55
    /**
56
     * Find an address by its id.
57
     *
58
     * @param int $id ID to be used to retrieve the address
59
     *
60
     * @throws \PWWeb\Localisation\Exceptions\AddressDoesNotExist
61
     */
62
    public static function findById(int $id): AddressContract
63
    {
64
        $address = static::getAddresses(['id' => $id])->first();
65
66
        if (null === $address) {
67
            throw AddressDoesNotExist::withId($id);
0 ignored issues
show
Bug introduced by
The type PWWeb\Localisation\Models\AddressDoesNotExist was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
68
        }
69
70
        return $address;
71
    }
72
73
    /**
74
     * Get the current cached addresses.
75
     *
76
     * @param array $params additional parameters for the database query
77
     *
78
     * @return Collection collection of addresses
79
     */
80
    protected static function getAddresses(array $params = []): Collection
81
    {
82
        return app(LocalisationRegistrar::class)
83
            ->setAddressClass(static::class)
84
            ->getAddresses($params);
85
    }
86
}
87