MapperFactory   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 6
Bugs 2 Features 0
Metric Value
wmc 2
c 6
b 2
f 0
lcom 1
cbo 3
dl 0
loc 69
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A make() 0 23 1
1
<?php
2
3
namespace Analogue\ORM\System;
4
5
use Analogue\ORM\EntityMap;
6
use Illuminate\Contracts\Events\Dispatcher;
7
use Analogue\ORM\Drivers\Manager as DriverManager;
8
9
/**
10
 * Build a mapper instance from an EntityMap object, doing the
11
 * required parsing of relationships. Abstracting to this class
12
 * will make it easy to later cache the EntityMap for better performances.
13
 */
14
class MapperFactory
15
{
16
    /**
17
     * Manager instance
18
     *
19
     * @var \Analogue\ORM\System\Manager
20
     */
21
    protected $manager;
22
23
    /**
24
     * DriverManager instance
25
     *
26
     * @var \Analogue\ORM\Drivers\Manager
27
     */
28
    protected $drivers;
29
30
    /**
31
     * Event dispatcher instance
32
     *
33
     * @var \Illuminate\Contracts\Events\Dispatcher
34
     */
35
    protected $dispatcher;
36
37
    /**
38
     * MapperFactory constructor.
39
     * @param DriverManager $drivers
40
     * @param Dispatcher    $dispatcher
41
     * @param Manager       $manager
42
     */
43
    public function __construct(DriverManager $drivers, Dispatcher $dispatcher, Manager $manager)
44
    {
45
        $this->drivers = $drivers;
46
47
        $this->dispatcher = $dispatcher;
48
49
        $this->manager = $manager;
50
    }
51
52
    /**
53
     * Return a new Mapper instance
54
     *
55
     * @param  string    $entityClass
56
     * @param  EntityMap $entityMap
57
     * @return Mapper
58
     */
59
    public function make($entityClass, EntityMap $entityMap)
0 ignored issues
show
Unused Code introduced by
The parameter $entityClass is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
60
    {
61
        $driver = $entityMap->getDriver();
62
        
63
        $connection = $entityMap->getConnection();
64
65
        $adapter = $this->drivers->getAdapter($driver, $connection);
66
        
67
        $entityMap->setDateFormat($adapter->getDateFormat());
68
69
        $mapper = new Mapper($entityMap, $adapter, $this->dispatcher, $this->manager);
0 ignored issues
show
Bug introduced by
It seems like $adapter defined by $this->drivers->getAdapter($driver, $connection) on line 65 can be null; however, Analogue\ORM\System\Mapper::__construct() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
70
71
        // Fire Initializing Event
72
        $mapper->fireEvent('initializing', $mapper);
0 ignored issues
show
Documentation introduced by
$mapper is of type object<Analogue\ORM\System\Mapper>, but the function expects a object<Analogue\ORM\Entity>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
73
        
74
        // Proceed necessary parsing on the EntityMap object
75
        $entityMap->initialize();
76
77
        // Fire Initialized Event
78
        $mapper->fireEvent('initialized', $mapper);
0 ignored issues
show
Documentation introduced by
$mapper is of type object<Analogue\ORM\System\Mapper>, but the function expects a object<Analogue\ORM\Entity>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
79
80
        return $mapper;
81
    }
82
}
83