Completed
Pull Request — master (#233)
by
unknown
10:59
created

Author::getEntryArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2
Metric Value
dl 0
loc 3
ccs 0
cts 0
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 2
crap 2
1
<?php
2
/**
3
 * COPS (Calibre OPDS PHP Server) class file
4
 *
5
 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6
 * @author     S�bastien Lucas <[email protected]>
7
 */
8
9
require_once('base.php');
10
11
class Author extends Base {
12
    const ALL_AUTHORS_ID = "cops:authors";
13
14
    const SQL_AUTHORS_BY_FIRST_LETTER = 
15
    	"select authors.id as id, authors.name as name, authors.sort as sort, count(*) as count 
16
    	 from authors
17
    		inner join books_authors_link as link on link.author = authors.id
18
    		inner join ({0}) as filter on filter.id = link.book
19
    	 where upper (authors.sort) like ? 
20
    	 group by authors.id, authors.name, authors.sort 
21
    	 order by sort";
22
    const SQL_AUTHORS_FOR_SEARCH = 
23 56
    	"select authors.id as id, authors.name as name, authors.sort as sort, count(*) as count 
24 56
    	 from authors
25 56
    		inner join books_authors_link as link on link.author = authors.id
26 56
    		inner join ({0}) as filter on filter.id = link.book 
27 56
    	 where (upper (authors.sort) like ? or upper (authors.name) like ?) 
28
    	 group by authors.id, authors.name, authors.sort 
29 53
    	 order by sort";
30 53
    const SQL_ALL_AUTHORS = 
31
    	"select authors.id as id, authors.name as name, authors.sort as sort, count(*) as count 
32
    	 from authors 
33 20
    		inner join books_authors_link as link on author = authors.id
34 20
    		inner join ({0}) as filter on filter.id = link.book 
35
    	 group by authors.id, authors.name, authors.sort 
36
    	 order by sort";
37 3
    const SQL_ALL_AUTHORS_FIRST_LETTERS = 
38 3
    	"select substr (upper (sort), 1, 1) as title, count(*) as count
39
		 from authors
40
    		inner join books_authors_link as link on link.author = authors.id
41 7
    		inner join ({0}) as filter on filter.id = link.book
42
		 group by substr (upper (sort), 1, 1)
43 7
		 order by substr (upper (sort), 1, 1)";
44
45
    public $id;
46 2
    public $name;
47 2
    public $sort;
48
49
    public function __construct($post) {
50 2
        $this->id = $post->id;
51 2
        $this->name = str_replace("|", ",", $post->name);
52 2
        $this->sort = $post->sort;
53
    }
54 2
55 2
    public function getUri () {
56 2
        return "?page=".parent::PAGE_AUTHOR_DETAIL."&id=$this->id";
57 2
    }
58 2
59
    public function getEntryId () {
60
        return self::ALL_AUTHORS_ID.":".$this->id;
61 1
    }
62 1
63
    public static function getEntryIdByLetter ($startingLetter) {
64
        return self::ALL_AUTHORS_ID.":letter:".$startingLetter;
65 23
    }
66 23
67
    public static function getCount() {
68
        // str_format (localize("authors.alphabetical", count(array))
69 1
        return parent::getCountGeneric ("authors", self::ALL_AUTHORS_ID, parent::PAGE_ALL_AUTHORS);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (getCountGeneric() instead of getCount()). Are you sure this is correct? If so, you might want to change this to $this->getCountGeneric().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
70 1
    }
71
72
    public static function getAllAuthorsFirstLetters() {
73 25
        list (, $result) = parent::executeFilteredQuery(self::SQL_ALL_AUTHORS_FIRST_LETTERS, array(), -1);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (executeFilteredQuery() instead of getAllAuthorsFirstLetters()). Are you sure this is correct? If so, you might want to change this to $this->executeFilteredQuery().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
74 25
        $entryArray = array();
75
        while ($post = $result->fetchObject ())
76
        {
77 7
            array_push ($entryArray, new Entry ($post->title, Author::getEntryIdByLetter ($post->title),
78 7
                str_format (localize("authorword", $post->count), $post->count), "text",
79 7
                array ( new LinkNavigation ("?page=".parent::PAGE_AUTHORS_FIRST_LETTER."&id=". rawurlencode ($post->title))), "", $post->count));
80 7
        }
81 7
        return $entryArray;
82
    }
83
84 46
    public static function getAuthorsByStartingLetter($letter) {
85 46
        return self::getEntryArray (self::SQL_AUTHORS_BY_FIRST_LETTER, array ($letter . "%"));
86
    }
87 46
88 46
    public static function getAuthorsForSearch($query) {
89 46
        return self::getEntryArray (self::SQL_AUTHORS_FOR_SEARCH, array ($query . "%", $query . "%"));
90 46
    }
91 46
92 46
    public static function getAllAuthors() {
93 46
        return self::getEntryArray (self::SQL_ALL_AUTHORS, array ());
94
    }
95
96
    public static function getEntryArray ($query, $params) {
97
        return Base::getEntryArrayWithBookNumber ($query, $params, "Author");
98
    }
99
100
    public static function getAuthorById ($authorId) {
101
        $result = parent::getDb ()->prepare('select authors.id as id, authors.name as name, authors.sort as sort, count(*) as count from authors where id = ?');
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (getDb() instead of getAuthorById()). Are you sure this is correct? If so, you might want to change this to $this->getDb().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
102
        $result->execute (array ($authorId));
103
        $post = $result->fetchObject ();
104
        return new Author ($post);
105
    }
106
107
    public static function getAuthorByBookId ($bookId) {
108
        $result = parent::getDb ()->prepare('select authors.id as id, authors.name as name, authors.sort as sort from authors, books_authors_link
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (getDb() instead of getAuthorByBookId()). Are you sure this is correct? If so, you might want to change this to $this->getDb().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
109
where author = authors.id
110
and book = ?');
111
        $result->execute (array ($bookId));
112
        $authorArray = array ();
113
        while ($post = $result->fetchObject ()) {
114
            array_push ($authorArray, new Author ($post));
115
        }
116
        return $authorArray;
117
    }
118
}
119