1 | <?php |
||
17 | class JobQueue extends Component implements BootstrapInterface |
||
18 | { |
||
19 | /** |
||
20 | * Available connections |
||
21 | * |
||
22 | * @var array |
||
23 | */ |
||
24 | public $connections = [ |
||
25 | 'thread' => [ |
||
26 | 'driver' => 'mongo-thread', |
||
27 | 'table' => 'yii_jobs_thread', |
||
28 | 'queue' => 'default', |
||
29 | 'expire' => 60, |
||
30 | 'limit' => 15, // Or 1 |
||
31 | 'connectionName' => 'thread', |
||
32 | ], |
||
33 | ]; |
||
34 | |||
35 | /** |
||
36 | * Encryption key |
||
37 | * |
||
38 | * @var string |
||
39 | */ |
||
40 | public $privateKey = 'rc5lgpue80sr17nx'; |
||
41 | |||
42 | /** |
||
43 | * Manager instance |
||
44 | * |
||
45 | * @var |
||
46 | */ |
||
47 | private $manager = null; |
||
48 | |||
49 | /** |
||
50 | * Initialize |
||
51 | * |
||
52 | * @param \yii\base\Application $app |
||
53 | */ |
||
54 | public function bootstrap($app) |
||
58 | |||
59 | /** |
||
60 | * Connect queue manager for mongo database |
||
61 | * |
||
62 | * @return Manager |
||
63 | */ |
||
64 | protected function connect() |
||
79 | |||
80 | /** |
||
81 | * Get queue manager instance |
||
82 | * |
||
83 | * @return mixed |
||
84 | */ |
||
85 | public function getQueueManager() |
||
89 | |||
90 | /** |
||
91 | * Push new job to queue |
||
92 | * |
||
93 | * @param mixed $job |
||
94 | * @param array $data |
||
95 | * @param string $queue |
||
96 | * @param string $connection |
||
97 | * |
||
98 | * @return mixed |
||
99 | */ |
||
100 | public static function push($job, $data = [], $queue = 'default', $connection = 'default') |
||
104 | |||
105 | /** |
||
106 | * Push new job to queue if this job is not exist |
||
107 | * |
||
108 | * @param mixed $job |
||
109 | * @param array $data |
||
110 | * @param string $queue |
||
111 | * @param string $connection |
||
112 | * |
||
113 | * @return mixed |
||
114 | */ |
||
115 | public static function pushUnique($job, $data = [], $queue = 'default', $connection = 'default') |
||
123 | |||
124 | /** |
||
125 | * Push a new an array of jobs onto the queue. |
||
126 | * |
||
127 | * @param array $jobs |
||
128 | * @param mixed $data |
||
129 | * @param string $queue |
||
130 | * @param string $connection |
||
131 | * |
||
132 | * @return mixed |
||
133 | */ |
||
134 | public static function bulk($jobs, $data = '', $queue = null, $connection = null) |
||
138 | |||
139 | /** |
||
140 | * Push a new job onto the queue after a delay. |
||
141 | * |
||
142 | * @param \DateTime|int $delay |
||
143 | * @param string $job |
||
144 | * @param mixed $data |
||
145 | * @param string $queue |
||
146 | * @param string $connection |
||
147 | * |
||
148 | * @return mixed |
||
149 | */ |
||
150 | public static function later($delay, $job, $data = '', $queue = null, $connection = null) |
||
154 | |||
155 | /** |
||
156 | * Push a new job into the queue after a delay if job does not exist. |
||
157 | * |
||
158 | * @param \DateTime|int $delay |
||
159 | * @param string $job |
||
160 | * @param mixed $data |
||
161 | * @param string $queue |
||
162 | * @param string $connection |
||
163 | * |
||
164 | * @return mixed |
||
165 | */ |
||
166 | public static function laterUnique($delay, $job, $data = '', $queue = null, $connection = null) |
||
174 | } |
||
175 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: