Completed
Push — master ( 144202...6a00f8 )
by Peter
11:59
created

ConfigureTrait   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 50%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 14
c 2
b 0
f 0
lcom 1
cbo 2
dl 0
loc 77
ccs 24
cts 48
cp 0.5
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
D configure() 0 68 14
getCriteria() 0 1 ?
1
<?php
2
3
/*
4
 * To change this license header, choose License Headers in Project Properties.
5
 * To change this template file, choose Tools | Templates
6
 * and open the template in the editor.
7
 */
8
9
namespace Maslosoft\Mangan\Traits\DataProvider;
10
11
use Maslosoft\Mangan\Interfaces\CriteriaInterface;
12
use Maslosoft\Mangan\Meta\ManganMeta;
13
14
/**
15
 * ConfigureTrait
16
 *
17
 * @author Piotr Maselkowski <pmaselkowski at gmail.com>
18
 */
19
trait ConfigureTrait
20
{
21
22 1
	protected function configure($modelClass, $config)
23
	{
24 1
		if (is_string($modelClass))
25 1
		{
26
			$this->model = new $modelClass;
0 ignored issues
show
Bug introduced by
The property model does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
27
		}
28 1
		elseif (is_object($modelClass))
29
		{
30 1
			$this->model = $modelClass;
31 1
		}
32
		else
33
		{
34
			throw new ManganException('Invalid model type for ' . static::class);
35
		}
36
37
38
		// Set criteria from model
39 1
		$criteria = $this->getCriteria();
40 1
		if ($criteria instanceof MergeableInterface)
0 ignored issues
show
Bug introduced by
The class Maslosoft\Mangan\Traits\...ider\MergeableInterface does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
41 1
		{
42
			// NOTE: WithCriteria and CriteriaAware have just slightly different method names
43
			if ($this->model instanceof WithCriteriaInterface)
0 ignored issues
show
Bug introduced by
The class Maslosoft\Mangan\Traits\...r\WithCriteriaInterface does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
44
			{
45
				$criteria->mergeWith($this->model->getDbCriteria());
46
			}
47
			elseif ($this->model instanceof CriteriaAwareInterface)
0 ignored issues
show
Bug introduced by
The class Maslosoft\Mangan\Traits\...\CriteriaAwareInterface does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
48
			{
49
				$criteria->mergeWith($this->model->getCriteria());
50
			}
51
		}
52
53
		// Merge criteria from configuration
54 1
		if (isset($config['criteria']))
55 1
		{
56
			$criteria->mergeWith($config['criteria']);
57
			unset($config['criteria']);
58
		}
59
60
		// Merge limit from configuration
61 1
		if (isset($config['limit']) && $config['limit'] > 0)
62 1
		{
63
			$criteria->setLimit($config['limit']);
64
			unset($config['limit']);
65
		}
66
67
		// Merge sorting from configuration
68 1
		if (isset($config['sort']))
69 1
		{
70
			// Apply default sorting if criteria does not have sort configured
71
			if (isset($config['sort']['defaultOrder']) && empty($this->getCriteria()->getSort()))
72
			{
73
				$criteria->setSort($config['sort']['defaultOrder']);
74
			}
75
			unset($config['sort']);
76
		}
77
78 1
		if (!$criteria->getSelect())
79 1
		{
80 1
			$fields = array_keys(ManganMeta::create($this->model)->fields());
81 1
			$selected = array_fill_keys($fields, true);
82 1
			$criteria->setSelect($selected);
83 1
		}
84
85 1
		foreach ($config as $key => $value)
86
		{
87
			$this->$key = $value;
88 1
		}
89 1
	}
90
91
	/**
92
	 * @return CriteriaInterface
93
	 */
94
	abstract public function getCriteria();
95
}
96