Test Setup Failed
Push — developer ( 4955d5...3912ea )
by Mariusz
16:57
created

SynchronizerPicklist   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
eloc 29
c 1
b 0
f 0
dl 0
loc 46
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B import() 0 36 9
1
<?php
2
/**
3
 * Synchronizer trait file for picklist.
4
 *
5
 * @package Controller
6
 *
7
 * @copyright YetiForce S.A.
8
 * @license   YetiForce Public License 5.0 (licenses/LicenseEN.txt or yetiforce.com)
9
 * @author    Mariusz Krzaczkowski <[email protected]>
10
 */
11
12
namespace App\Integrations\Traits;
13
14
/**
15
 * Synchronizer trait for picklist.
16
 */
17
trait SynchronizerPicklist
18
{
19
	/** @var int[] */
20
	private $roleIdList = [];
21
22
	/**
23
	 * Import account type from API.
24
	 *
25
	 * @return void
26
	 */
27
	public function import(): void
28
	{
29
		if ($this->config->get('log_all')) {
30
			$this->controller->log('Start import ' . $this->name, []);
31
		}
32
		$isRoleBased = $this->fieldModel->isRoleBased();
33
		$values = $this->getPicklistValues();
0 ignored issues
show
Bug introduced by
It seems like getPicklistValues() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

33
		/** @scrutinizer ignore-call */ 
34
  $values = $this->getPicklistValues();
Loading history...
34
		$i = 0;
35
		foreach ($this->cache as $key => $value) {
0 ignored issues
show
Bug introduced by
The property cache does not exist on App\Integrations\Traits\SynchronizerPicklist. Did you mean cacheList?
Loading history...
36
			if (empty($value)) {
37
				continue;
38
			}
39
			$name = mb_strtolower($value);
40
			if (empty($values[$name])) {
41
				try {
42
					$itemModel = $this->fieldModel->getItemModel();
43
					$itemModel->validateValue('name', $value);
44
					$itemModel->set('name', $value);
45
					if ($isRoleBased) {
46
						if (empty($this->roleIdList)) {
47
							$this->roleIdList = array_keys(\Settings_Roles_Record_Model::getAll());
48
						}
49
						$itemModel->set('roles', $this->roleIdList);
50
					}
51
					$itemModel->save();
52
					$this->cacheList[$value] = $key;
0 ignored issues
show
Bug Best Practice introduced by
The property cacheList does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
53
					++$i;
54
				} catch (\Throwable $ex) {
55
					$this->logError('import ' . $this->name, ['API' => $value], $ex);
0 ignored issues
show
Bug introduced by
It seems like logError() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

55
					$this->/** @scrutinizer ignore-call */ 
56
            logError('import ' . $this->name, ['API' => $value], $ex);
Loading history...
56
				}
57
			} else {
58
				$this->cacheList[$values[$name]] = $key;
59
			}
60
		}
61
		if ($this->config->get('log_all')) {
62
			$this->controller->log('End import ' . $this->name, ['imported' => $i]);
63
		}
64
	}
65
}
66