Completed
Push — develop ( 4bf430...c74260 )
by
unknown
18:30
created

Categories   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 1
dl 0
loc 43
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A findOneBy() 0 6 2
A findBy() 0 6 2
B createDefaultCategory() 0 18 5
1
<?php
2
/**
3
 * YAWIK
4
 *
5
 * @filesource
6
 * @license MIT
7
 * @copyright  2013 - 2017 Cross Solution <http://cross-solution.de>
8
 */
9
  
10
/** */
11
namespace Jobs\Repository;
12
13
use Core\Repository\AbstractRepository;
14
15
/**
16
 * Repository for job categories.
17
 *
18
 * Creates default categories upon first access, if not present.
19
 * 
20
 * @author Mathias Gelhausen <[email protected]>
21
 * @since 0.29
22
 */
23
class Categories extends AbstractRepository
24
{
25
26
    public function findOneBy(array $criteria)
27
    {
28
        $category = parent::findOneBy($criteria);
29
30
        return $category ?: $this->createDefaultCategory($criteria);
31
    }
32
33
    public function findBy(array $criteria, array $sort = null, $limit = null, $skip = null)
34
    {
35
        $categories = parent::findBy($criteria, $sort, $limit, $skip);
36
37
        return empty($categories) ? [$this->createDefaultCategory($criteria)] : $categories;
38
    }
39
40
    /**
41
     * Creates and stores the default category hirarchy for the given value.
42
     *
43
     * @param array|string $value
44
     *
45
     * @return null|\Jobs\Entity\Category
46
     */
47
    public function createDefaultCategory($value)
48
    {
49
        if (is_array($value)) {
50
            $value = isset($value['value']) ? $value['value'] : '';
51
        }
52
53
        if ('professions' != $value && 'employmentTypes' != $value) {
54
            return null;
55
        }
56
57
        $builder = $this->getService('Jobs/DefaultCategoriesBuilder');
58
59
        $category = $builder->build($value);
60
61
        $this->store($category);
62
63
        return $category;
64
    }
65
}