Completed
Push — master ( 8bfd95...2626ba )
by Dan Michael O.
12:11
created

ImportRecord   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 4
c 0
b 0
f 0
lcom 1
cbo 6
dl 0
loc 41
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A handle() 0 5 1
A imported() 0 11 2
1
<?php
2
3
namespace Colligator\Jobs;
4
5
use Colligator\Collection;
6
use Colligator\Document;
7
use Colligator\Marc21Importer;
8
use Colligator\Search\DocumentsIndex;
9
use Danmichaelo\QuiteSimpleXMLElement\QuiteSimpleXMLElement;
10
11
class ImportRecord extends Job
12
{
13
    protected $record;
14
    protected $collection;
15
16
    /**
17
     * ImportRecord constructor.
18
     * @param Collection $collection
19
     * @param QuiteSimpleXMLElement $record
20
     */
21
    public function __construct(Collection $collection, QuiteSimpleXMLElement $record)
22
    {
23
        $this->collection = $collection;
24
        $this->record = $record;
25
    }
26
27
    /**
28
     * @param Marc21Importer $importer
29
     */
30
    public function handle(DocumentsIndex $docIndex, Marc21Importer $importer)
31
    {
32
        $docId = $importer->import($this->record);
33
        $this->imported($docIndex, $docId);
34
    }
35
36
    /**
37
     * @param DocumentsIndex $docIndex
38
     * @param int $docId
39
     */
40
    public function imported($docIndex, $docId)
41
    {
42
        $doc = Document::with('subjects', 'genres', 'cover')->find($docId);
0 ignored issues
show
Bug introduced by
The method find does only exist in Illuminate\Database\Eloquent\Builder, but not in Illuminate\Database\Eloquent\Model.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
43
44
        if (!$this->collection->documents->contains($doc->id)) {
0 ignored issues
show
Documentation introduced by
The property documents does not exist on object<Colligator\Collection>. 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...
45
            $this->collection->documents()->attach($doc->id);
46
        }
47
48
        // Add/update ElasticSearch
49
        $docIndex->index($doc);
50
    }
51
}