Completed
Push — master ( e08a79...5de93f )
by Raphaël
03:21
created

ProfileCertain::getProfileByEmail()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 12
rs 9.4285
cc 3
eloc 8
nc 3
nop 1
1
<?php
2
namespace Wabel\CertainAPI\Ressources;
3
4
use Wabel\CertainAPI\Interfaces\CertainRessourceInterface;
5
use Wabel\CertainAPI\CertainRessourceAbstract;
6
use Wabel\CertainAPI\Exceptions\RessourceException;
7
/**
8
 * ProfileCertain about the Profile entity
9
 *
10
 * @author rbergina
11
 */
12
class ProfileCertain extends CertainRessourceAbstract implements CertainRessourceInterface
13
{
14
    public function getRessourceName(){
15
        return 'Profile';
16
    }
17
    public function getMandatoryFields()
18
    {
19
        return array();
20
    }
21
22
    /**
23
     * Return the Profile object
24
     * @param string $email
25
     * @return ProfileObj
26
     * @throws RessourceException
27
     */
28
    public function getProfileByEmail($email)
29
    {
30
        $resultCertain =  $this->getProfileCertainReturnByEmail($email);
31
        if($resultCertain->size == 1){
32
            return $resultCertain->profiles[0];
0 ignored issues
show
Bug introduced by
The property profiles does not seem to exist in Wabel\CertainAPI\CertainRessourceAbstract.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
33
            
34
        } elseif($resultCertain->size > 1){
0 ignored issues
show
Bug introduced by
The property size does not seem to exist in Wabel\CertainAPI\CertainRessourceAbstract.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
35
            throw new RessourceException('Duplicate entries');
36
        } else{
37
                return $resultCertain;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $resultCertain; (Wabel\CertainAPI\CertainRessourceAbstract) is incompatible with the return type documented by Wabel\CertainAPI\Ressour...tain::getProfileByEmail of type Wabel\CertainAPI\Ressources\ProfileObj.

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:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
38
        }
39
    }
40
41
    public function getProfileCertainReturnByEmail($email)
42
    {
43
        return $this->get(null, ['email'=> $email]);
44
    }
45
}