Completed
Push — master ( 563916...ca1336 )
by Adrien
05:31
created

search_movie_source.php ➔ fetchMovieData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 7
c 2
b 0
f 0
nc 1
nop 2
dl 0
loc 12
rs 9.4285
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 14 and the first side effect is on line 6.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
3
/**
4
 * This script will search for source for needed movies that does not have sources yet
5
 */
6
require_once __DIR__ . '/../public/index.php';
7
8
/**
9
 * Process a movie and pause between each of them
10
 * @param Closure $func
11
 * @param \mQueue\Model\Movie[] $movies
12
 * @param int $seconds
13
 */
14
function movieProcessor(Closure $func, $movies, $seconds)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
15
{
16
    $total = $movies->count();
1 ignored issue
show
Bug introduced by
The method count cannot be called on $movies (of type array<integer,object<mQueue\Model\Movie>>).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
17
    $count = 0;
18
    foreach ($movies as $movie) {
19
        echo '[' . str_pad(++$count, 5, ' ', STR_PAD_LEFT) . '/' . str_pad($total, 5, ' ', STR_PAD_LEFT) . '] ' . $movie->getImdbUrl('akas') . "\t";
20
        flush();
21
22
        $func($movie);
23
24
        echo "\n";
25
26
        sleep($seconds);
27
    }
28
29
    return $total;
30
}
31
32
/**
33
 * Search source
34
 */
35
function searchSource()
36
{
37
    $movies = \mQueue\Model\MovieMapper::findAllForSearch();
38
    $searcher = function (\mQueue\Model\Movie $movie) {
39
        $searchEngine = new mQueue\Service\SearchEngine();
40
41
        $movie->fetchData(); // Refresh movie data to be sure we have latest available title
42
        $title = $movie->getTitle();
43
        $data = $searchEngine->search($title);
44
        $scores = $searchEngine->computeScores($title, $data);
45
46
        $best = @reset($scores);
47
        $movie->setSource($best);
48
        $movie->save();
49
50
        echo $movie->source ? : '[source not found]';
0 ignored issues
show
Documentation introduced by
The property source does not exist on object<mQueue\Model\Movie>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
51
    };
52
53
    // 5 minutes pause between search, not to stress third-party servers
54
    $total = movieProcessor($searcher, $movies, 5 * 60);
55
56
    echo $total . " movie sources updated in database\n";
57
}
58
59
/**
60
 * Fetch movie data for those that need it
61
 * @param int $limit
0 ignored issues
show
Documentation introduced by
Should the type for parameter $limit not be integer|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
62
 * @param int $sleep seconds to sleep between each movie
63
 */
64
function fetchMovieData($limit = null, $sleep = 0)
65
{
66
    $movies = \mQueue\Model\MovieMapper::findAllForFetching($limit);
67
    $fetcher = function (\mQueue\Model\Movie $movie) {
68
        $movie->fetchData();
69
        echo $movie->title;
0 ignored issues
show
Documentation introduced by
The property title does not exist on object<mQueue\Model\Movie>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
70
    };
71
72
    $total = movieProcessor($fetcher, $movies, $sleep);
73
74
    echo $total . " movies updated in database\n";
75
}
76
77
// we only do things if this file were NOT included (otherwise, the file was included to access misc functions)
78
if (realpath(__FILE__) == realpath($_SERVER['SCRIPT_FILENAME'])) {
79
    // Clean up obsolete sources
80
    \mQueue\Model\MovieMapper::deleteObsoleteSources();
81
82
    // Fetch movie data to update title and release date
83
    fetchMovieData(20, 1 * 60);
84
85
    // Search source for movies with release date
86
    searchSource();
87
}
88