LazyOption::randomOnes()   A
last analyzed

Complexity

Conditions 4
Paths 1

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 9.584
c 0
b 0
f 0
nc 1
cc 4
nop 2
1
<?php
2
3
/*
4
 * This file is part of AppName.
5
 *
6
 * (c) Monofony
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace App\Fixture\OptionsResolver;
13
14
use Doctrine\Common\Collections\Collection;
15
use Sylius\Component\Resource\Repository\RepositoryInterface;
16
use Symfony\Component\OptionsResolver\Options;
17
use Webmozart\Assert\Assert;
18
19
/**
20
 * Using the hacky hack to distinct between option which wasn't set
21
 * and option which was set to empty.
22
 *
23
 * Usage:
24
 *
25
 *   $optionsResolver
26
 *     ->setDefault('option', LazyOption::randomOne($repository))
27
 *     ->setNormalizer('option', LazyOption::findOneBy($repository, 'code'))
28
 *   ;
29
 *
30
 *   Returns:
31
 *     - null if user explicitly set it (['option' => null])
32
 *     - random one if user skipped that option ([])
33
 *     - specific one if user defined that option (['option' => 'CODE'])
34
 */
35
final class LazyOption
36
{
37
    /**
38
     * @param RepositoryInterface $repository
39
     *
40
     * @return \Closure
41
     */
42
    public static function randomOne(RepositoryInterface $repository)
43
    {
44
        return function (Options $options) use ($repository) {
0 ignored issues
show
Unused Code introduced by
The parameter $options 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...
45
            $objects = $repository->findAll();
46
47
            if ($objects instanceof Collection) {
48
                $objects = $objects->toArray();
49
            }
50
51
            Assert::notEmpty($objects);
52
53
            return $objects[array_rand($objects)];
54
        };
55
    }
56
57
    /**
58
     * @param RepositoryInterface $repository
59
     * @param int                 $chanceOfRandomOne
60
     *
61
     * @return \Closure
62
     */
63
    public static function randomOneOrNull(RepositoryInterface $repository, $chanceOfRandomOne)
64
    {
65
        return function (Options $options) use ($repository, $chanceOfRandomOne) {
0 ignored issues
show
Unused Code introduced by
The parameter $options 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...
66
            if (mt_rand(1, 100) > $chanceOfRandomOne) {
67
                return null;
68
            }
69
70
            $objects = $repository->findAll();
71
72
            if ($objects instanceof Collection) {
73
                $objects = $objects->toArray();
74
            }
75
76
            return 0 === count($objects) ? null : $objects[array_rand($objects)];
77
        };
78
    }
79
80
    /**
81
     * @param RepositoryInterface $repository
82
     * @param int                 $amount
83
     *
84
     * @return \Closure
85
     */
86
    public static function randomOnes(RepositoryInterface $repository, $amount)
87
    {
88
        return function (Options $options) use ($repository, $amount) {
0 ignored issues
show
Unused Code introduced by
The parameter $options 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...
89
            $objects = $repository->findAll();
90
91
            if ($objects instanceof Collection) {
92
                $objects = $objects->toArray();
93
            }
94
95
            $selectedObjects = [];
96
            for (; $amount > 0 && count($objects) > 0; --$amount) {
97
                $randomKey = array_rand($objects);
98
99
                $selectedObjects[] = $objects[$randomKey];
100
101
                unset($objects[$randomKey]);
102
            }
103
104
            return $selectedObjects;
105
        };
106
    }
107
108
    /**
109
     * @param RepositoryInterface $repository
110
     *
111
     * @return \Closure
112
     */
113
    public static function all(RepositoryInterface $repository)
114
    {
115
        return function (Options $options) use ($repository) {
0 ignored issues
show
Unused Code introduced by
The parameter $options 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...
116
            return $repository->findAll();
117
        };
118
    }
119
120
    /**
121
     * @param RepositoryInterface $repository
122
     * @param string              $field
123
     *
124
     * @return \Closure
125
     */
126
    public static function findBy(RepositoryInterface $repository, $field)
127
    {
128
        return function (Options $options, $previousValues) use ($repository, $field) {
129
            if (null === $previousValues || [] === $previousValues) {
130
                return $previousValues;
131
            }
132
133
            Assert::isArray($previousValues);
134
135
            $resources = [];
136
            foreach ($previousValues as $previousValue) {
137
                if (is_object($previousValue)) {
138
                    $resources[] = $previousValue;
139
                } else {
140
                    $resources[] = $repository->findOneBy([$field => $previousValue]);
141
                }
142
            }
143
144
            return $resources;
145
        };
146
    }
147
148
    /**
149
     * @param RepositoryInterface $repository
150
     * @param string              $field
151
     *
152
     * @return \Closure
153
     */
154
    public static function findOneBy(RepositoryInterface $repository, $field)
155
    {
156
        return function (Options $options, $previousValue) use ($repository, $field) {
157
            if (null === $previousValue || [] === $previousValue) {
158
                return $previousValue;
159
            }
160
161
            if (is_object($previousValue)) {
162
                return $previousValue;
163
            } else {
164
                return $repository->findOneBy([$field => $previousValue]);
165
            }
166
        };
167
    }
168
}
169