diff --git a/library/Class/Users.php b/library/Class/Users.php index be6e2ddaac7b6360148d2ea241edf3903c409571..8e321a227e4285859ca9c496c5006fe7d1000f59 100644 --- a/library/Class/Users.php +++ b/library/Class/Users.php @@ -1196,7 +1196,7 @@ class Class_Users extends Storm_Model_Abstract { if (!($emprunteur = $this->getEmprunteur())) return new Storm_Collection(); - $history = $emprunteur->getLoansHistory()->fetchAll(); + $history = $emprunteur->getLoansHistory(); $loans = $history->getLoans() ->select(function($loan) {return $loan->getDateRetour();}); diff --git a/library/Class/WebService/SIGB/AbstractService.php b/library/Class/WebService/SIGB/AbstractService.php index 76713763028a952d44241dec242665455b0433dd..8a18303dc054214a988fdbdebd1312c005fb9f05 100644 --- a/library/Class/WebService/SIGB/AbstractService.php +++ b/library/Class/WebService/SIGB/AbstractService.php @@ -129,8 +129,8 @@ abstract class Class_WebService_SIGB_AbstractService { } - public function loansHistory($emprunteur, $page = 1) { - return new Class_WebService_SIGB_LoansHistory(); + public function loansHistory($borrower) { + return new Class_WebService_SIGB_LoansHistory($borrower); } diff --git a/library/Class/WebService/SIGB/Koha/LoansReader.php b/library/Class/WebService/SIGB/Koha/LoansReader.php index ca15e78b2615479aa35262611999a72a05ee537f..0e92555a6c5d6b7dc09b3e9ccdce1ca18c5ea83e 100644 --- a/library/Class/WebService/SIGB/Koha/LoansReader.php +++ b/library/Class/WebService/SIGB/Koha/LoansReader.php @@ -21,13 +21,19 @@ class Class_WebService_SIGB_Koha_LoansReader { + protected $_borrower; + + + public function __construct($borrower) { + $this->_borrower = $borrower; + } + public function parse($json) { $loans = ($datas = json_decode($json)) ? array_map([$this, '_parseOne'], $datas) : []; - return (new Class_WebService_SIGB_LoansHistory()) - ->setLoans($loans) - ->setTotalCount(count($loans)); + return (new Class_WebService_SIGB_LoansHistory($this->_borrower)) + ->setLoans($loans); } diff --git a/library/Class/WebService/SIGB/Koha/RestfulService.php b/library/Class/WebService/SIGB/Koha/RestfulService.php index 476d9a0c52466fb62bf155ae5c86c5ebf6775e59..2749913271f1cd225456899a45c77ad2509148df 100644 --- a/library/Class/WebService/SIGB/Koha/RestfulService.php +++ b/library/Class/WebService/SIGB/Koha/RestfulService.php @@ -127,14 +127,14 @@ class Class_WebService_SIGB_Koha_RestfulService } - public function getLoansHistoryForUser($emprunteur) { - $content = $this->restfulGet('user/byid/' . $emprunteur->getId() . '/issues_history', []); + public function loansHistory($borrower) { + $content = $this->restfulGet('user/byid/' . $borrower->getId() . '/issues_history', []); if (null === json_decode($content)) throw new Class_WebService_Exception($this->_('Échec de la connexion au webservice, le SIGB a répondu "%s"', trim($content))); - return (new Class_WebService_SIGB_Koha_LoansReader()) + return (new Class_WebService_SIGB_Koha_LoansReader($borrower)) ->parse($content); } diff --git a/library/Class/WebService/SIGB/Koha/Service.php b/library/Class/WebService/SIGB/Koha/Service.php index 2ac8f8f537129552e87775ca3f5cda494aff3e92..ef149626b88004bd4713a97c56a2e2ee4c0066c7 100644 --- a/library/Class/WebService/SIGB/Koha/Service.php +++ b/library/Class/WebService/SIGB/Koha/Service.php @@ -198,16 +198,13 @@ class Class_WebService_SIGB_Koha_Service extends Class_WebService_SIGB_AbstractR } - public function loansHistory($user, $page = 1) { + public function loansHistory($borrower) { if (!$this->providesLoansHistory()) - return parent::loansHistory($user, $page); - - if ($page > 1) - return new Class_WebService_SIGB_LoansHistory(); + return parent::loansHistory($borrower); return $this ->getRestfulService() - ->getLoansHistoryForUser($user); + ->loansHistory($borrower); } diff --git a/library/Class/WebService/SIGB/LoansHistory.php b/library/Class/WebService/SIGB/LoansHistory.php index c2ecbb3cf9c21b59d60dade5910e200c14cd140f..04177c6af1a418ec0b92100742f99827761d9758 100644 --- a/library/Class/WebService/SIGB/LoansHistory.php +++ b/library/Class/WebService/SIGB/LoansHistory.php @@ -26,8 +26,9 @@ class Class_WebService_SIGB_LoansHistory { $_borrower, $_loans; - public function __construct() { + public function __construct($borrower) { $this->_loans = new Storm_Collection(); + $this->setBorrower($borrower); } @@ -47,12 +48,6 @@ class Class_WebService_SIGB_LoansHistory { } - public function setTotalCount($count) { - $this->_total_count = $count; - return $this; - } - - public function addLoan($loan) { $this->_loans->append($loan); return $this; @@ -65,20 +60,13 @@ class Class_WebService_SIGB_LoansHistory { } - public function fetchAll() { - if (!$this->_total_count) - return $this; - - if (!$this->_borrower) - return $this; + public function isEmpty() { + return $this->_loans->isEmpty(); + } - $current_page = 1; - do { - $current_page = $current_page + 1; - $loans = $this->_borrower->getLoansHistory($current_page)->getLoans(); - $this->_loans->addAll($loans); - } while(!$loans->isEmpty() and ($this->_loans->count() < $this->_total_count)); + public function addFrom($loans) { + $this->_loans->addAll($loans->getLoans()); return $this; } } diff --git a/library/Class/WebService/SIGB/Nanook/GetLoanHistoryReader.php b/library/Class/WebService/SIGB/Nanook/GetLoanHistoryReader.php index 38214968c26a48e1b4937d9d97a71e4013a4085a..d87c94bacb99bf255318f831b078168ae638d6ea 100644 --- a/library/Class/WebService/SIGB/Nanook/GetLoanHistoryReader.php +++ b/library/Class/WebService/SIGB/Nanook/GetLoanHistoryReader.php @@ -26,24 +26,22 @@ class Class_WebService_SIGB_Nanook_GetLoanHistoryReader { $_current_loan; + public function __construct($borrower) { + $this->_loans_history = new Class_WebService_SIGB_LoansHistory($borrower); + } + + /** * @param string $xml * @return Class_WebService_SIGB_*_PatronInfoReader */ public function parseXML($xml) { - $this->_loans_history = new Class_WebService_SIGB_LoansHistory(); $this->_xml_parser = Class_WebService_SIGB_Nanook_XMLParser::newInstance()->setElementHandler($this); $this->_xml_parser->parse($xml); - return $this; - } - - - public function startLoans($attributes) { - $this->_loans_history->setTotalCount($attributes['COUNT']); + return $this->_loans_history; } - public function startLoan() { $this->_current_loan = Class_WebService_SIGB_Emprunt::newInstanceWithEmptyExemplaire(); $this->_loans_history->addLoan($this->_current_loan); @@ -70,11 +68,6 @@ class Class_WebService_SIGB_Nanook_GetLoanHistoryReader { } - public function getHistory() { - return $this->_loans_history; - } - - public function endLocationLabel($data) { $this->_current_loan->setBibliotheque($data); } diff --git a/library/Class/WebService/SIGB/Nanook/Service.php b/library/Class/WebService/SIGB/Nanook/Service.php index 5e8b3096d6196e0b89b7ec0bc2a6db816a060639..81d8f538fcdfa818803b42d1be1de12779a45b15 100644 --- a/library/Class/WebService/SIGB/Nanook/Service.php +++ b/library/Class/WebService/SIGB/Nanook/Service.php @@ -148,16 +148,31 @@ class Class_Webservice_SIGB_Nanook_Service extends Class_WebService_SIGB_Abstrac } - public function loansHistory($emprunteur, $page = 1) { + protected function _fetchHistoryPage($borrower, $page) { + $this->log($this); $url = $this->buildQueryURL(['service' => 'GetLoanHistory', - 'patronId' => $emprunteur->getId()]) + 'patronId' => $borrower->getId()]) .'?pageNumber='.$page; $xml = $this->getWebClient()->open_url($url); - $this->log($this); - $history = (new Class_WebService_SIGB_Nanook_GetLoanHistoryReader()) - ->parseXML($xml) - ->getHistory(); - return $history->setBorrower($emprunteur); + return (new Class_WebService_SIGB_Nanook_GetLoanHistoryReader($borrower)) + ->parseXML($xml); + + } + + + public function loansHistory($borrower) { + $page = 1; + $all = new Class_WebService_SIGB_LoansHistory($borrower); + if (!$borrower) + return $all; + + while (($loans = $this->_fetchHistoryPage($borrower,$page)) && + !$loans->isEmpty()) { + $all->addFrom($loans); + $page++; + } + + return $all; } diff --git a/tests/fixtures/NanookFixtures.php b/tests/fixtures/NanookFixtures.php index cf2d9753aad6c93388b13bec67479f4983e8be97..52eeb9a5b3b62ac65a47cb3253b28d83537148cf 100644 --- a/tests/fixtures/NanookFixtures.php +++ b/tests/fixtures/NanookFixtures.php @@ -653,6 +653,12 @@ class NanookFixtures { </GetLoanHistory>'; } + public static function xmlGetLoanHistoryPageThree() { + return '<GetLoanHistory> + <loans count="3" pageNumber="3" pageSize="2"/> + </GetLoanHistory>'; + } + public static function xmlGetRecordsEx() { return '<GetRecords><record><bibId>369667</bibId><title>Barbara</title><items><item><barcode>028111272</barcode><itemId>235195</itemId><available>1</available><holdable>1</holdable><holds>0</holds><visible>1</visible><locationLabel>Saint Pargoire</locationLabel><locationId>21</locationId><activityMessage>En rayon</activityMessage></item></items></record></GetRecords>'; diff --git a/tests/library/Class/WebService/SIGB/KohaRestfulTest.php b/tests/library/Class/WebService/SIGB/KohaRestfulTest.php index 96a4c5f9b0b09d5f41401a326ae09ab5a2bc9d00..b7e943a2759c7f5b68514d7a30922e5199c23c41 100644 --- a/tests/library/Class/WebService/SIGB/KohaRestfulTest.php +++ b/tests/library/Class/WebService/SIGB/KohaRestfulTest.php @@ -414,7 +414,7 @@ class KohaRestGetUserHistoryTest extends KohaRestfulTestCase { /** @test */ public function userShouldHaveThreeLoans() { - $this->assertEquals(3, $this->loans_history->totalCount()); + $this->assertCount(3, $this->loans_history->getLoans()); } diff --git a/tests/library/Class/WebService/SIGB/NanookTest.php b/tests/library/Class/WebService/SIGB/NanookTest.php index 353842200898224b4966a9d330eace943748014e..f93bdee9d592c4e438fbffd354b791e88c682e28 100644 --- a/tests/library/Class/WebService/SIGB/NanookTest.php +++ b/tests/library/Class/WebService/SIGB/NanookTest.php @@ -1298,6 +1298,9 @@ class NanookLoanHistoryTest extends NanookTestCase { ->whenCalled('open_url') ->with('http://localhost:8080/afi_Nanook/ilsdi/service/GetLoanHistory/patronId/34?pageNumber=2') ->answers(NanookFixtures::xmlGetLoanHistoryPageTwo()) + ->whenCalled('open_url') + ->with('http://localhost:8080/afi_Nanook/ilsdi/service/GetLoanHistory/patronId/34?pageNumber=3') + ->answers(NanookFixtures::xmlGetLoanHistoryPageThree()) ->beStrict(); $this->emprunteur = (new Class_WebService_SIGB_Emprunteur('34', 'harlock')) ->setService($this->_service); @@ -1319,7 +1322,7 @@ class NanookLoanHistoryTest extends NanookTestCase { /** @test */ public function userShouldHaveThreeLoans() { - $this->assertEquals(3, $this->loans_history->totalCount()); + $this->assertEquals(3, count($this->loans_history->getLoans())); } @@ -1359,21 +1362,14 @@ class NanookLoanHistoryTest extends NanookTestCase { /** @test */ - public function secondLoanIssueDateShouldBe24_08_2017() { + public function firstLoanIssueDateShouldBe24_08_2017() { $this->assertEquals('24/08/2017', $this->loans_history->getLoans()[0]->getIssueDate()); } /** @test */ - public function secondPageLoanTitleShouldBeJeteCroquerai() { - $this->assertEquals('Je te croquerai !', $this->_service->loansHistory($this->emprunteur, 2)->getLoans()[0]->getTitre()); - } - - - /** @test */ - public function loanHistorFetchAllShouldLoadAllPages() { - $history = $this->_service->loansHistory($this->emprunteur)->fetchAll(); - $this->assertCount(3, $history->getLoans()); + public function thirdLoanTitleShouldBeJeteCroquerai() { + $this->assertEquals('Je te croquerai !', $this->loans_history->getLoans()[2]->getTitre()); } @@ -1386,6 +1382,4 @@ class NanookLoanHistoryTest extends NanookTestCase { $this->assertFalse($this->_service->providesLoansHistory()); } - - } \ No newline at end of file