Completed
Pull Request — develop (#323)
by
unknown
07:26
created

Categories::findBy()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 4
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
}