|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @copyright Copyright (c) 2017, Matias De lellis <[email protected]> |
|
4
|
|
|
* @copyright Copyright (c) 2018, Branko Kokanovic <[email protected]> |
|
5
|
|
|
* |
|
6
|
|
|
* @author Branko Kokanovic <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* @license GNU AGPL version 3 or any later version |
|
9
|
|
|
* |
|
10
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
11
|
|
|
* it under the terms of the GNU Affero General Public License as |
|
12
|
|
|
* published by the Free Software Foundation, either version 3 of the |
|
13
|
|
|
* License, or (at your option) any later version. |
|
14
|
|
|
* |
|
15
|
|
|
* This program is distributed in the hope that it will be useful, |
|
16
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
17
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
18
|
|
|
* GNU Affero General Public License for more details. |
|
19
|
|
|
* |
|
20
|
|
|
* You should have received a copy of the GNU Affero General Public License |
|
21
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
22
|
|
|
* |
|
23
|
|
|
*/ |
|
24
|
|
|
namespace OCA\FaceRecognition\BackgroundJob\Tasks; |
|
25
|
|
|
|
|
26
|
|
|
use OCP\IDBConnection; |
|
27
|
|
|
|
|
28
|
|
|
use OCA\FaceRecognition\Db\Image; |
|
29
|
|
|
use OCA\FaceRecognition\Db\ImageMapper; |
|
30
|
|
|
use OCA\FaceRecognition\BackgroundJob\FaceRecognitionBackgroundTask; |
|
31
|
|
|
use OCA\FaceRecognition\BackgroundJob\FaceRecognitionContext; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Task that gets all images (from database) that don't yet have faces found (e.g. they are not processed). |
|
35
|
|
|
* Shuffles found images and outputs them to context->propertyBag. |
|
36
|
|
|
*/ |
|
37
|
|
|
class EnumerateImagesMissingFacesTask extends FaceRecognitionBackgroundTask { |
|
38
|
|
|
/** @var IDBConnection DB connection*/ |
|
39
|
|
|
protected $connection; |
|
40
|
|
|
|
|
41
|
|
|
/** @var ImageMapper Image mapper*/ |
|
42
|
|
|
protected $imageMapper; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @param IDBConnection $connection DB connection |
|
46
|
|
|
* @param ImageMapper $imageMapper Image mapper |
|
47
|
|
|
*/ |
|
48
|
|
|
public function __construct(IDBConnection $connection, ImageMapper $imageMapper) { |
|
49
|
|
|
parent::__construct(); |
|
50
|
|
|
$this->connection = $connection; |
|
51
|
|
|
$this->imageMapper = $imageMapper; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @inheritdoc |
|
56
|
|
|
*/ |
|
57
|
|
|
public function description() { |
|
58
|
|
|
return "Find all images which don't have faces generated for them"; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @inheritdoc |
|
63
|
|
|
*/ |
|
64
|
|
|
public function execute(FaceRecognitionContext $context) { |
|
65
|
|
|
$this->setContext($context); |
|
66
|
|
|
|
|
67
|
|
|
$images = $this->imageMapper->findImagesWithoutFaces($this->context->user); |
|
68
|
|
|
yield; |
|
69
|
|
|
|
|
70
|
|
|
shuffle($images); |
|
71
|
|
|
$this->context->propertyBag['images'] = $images; |
|
72
|
|
|
|
|
73
|
|
|
return true; |
|
|
|
|
|
|
74
|
|
|
} |
|
75
|
|
|
} |
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.