Skip to content

Commit e44df66

Browse files
authored
Merge pull request 5am-code#14 from 5am-code/feature/refactor-collections
refactor collections (return EntityCollection, if list is fetched)
2 parents ffa4145 + feb000e commit e44df66

35 files changed

+1373
-244
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
.idea
22
vendor
3-
.phpunit.result.cache
3+
.phpunit.result.cache
4+
coverage/
5+
.phpunit.cache/

phpunit.xml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.5/phpunit.xsd"
4+
bootstrap="vendor/autoload.php"
5+
cacheResultFile=".phpunit.cache/test-results"
6+
executionOrder="depends,defects"
7+
forceCoversAnnotation="false"
8+
beStrictAboutCoversAnnotation="true"
9+
beStrictAboutOutputDuringTests="true"
10+
beStrictAboutTodoAnnotatedTests="true"
11+
failOnRisky="true"
12+
failOnWarning="true"
13+
verbose="true">
14+
<testsuites>
15+
<testsuite name="default">
16+
<directory suffix="Test.php">tests</directory>
17+
</testsuite>
18+
</testsuites>
19+
20+
<coverage cacheDirectory=".phpunit.cache/code-coverage"
21+
processUncoveredFiles="true">
22+
<include>
23+
<directory suffix=".php">src</directory>
24+
</include>
25+
</coverage>
26+
</phpunit>

src/Endpoints/Block.php

Lines changed: 5 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
namespace FiveamCode\LaravelNotionApi\Endpoints;
44

5-
use FiveamCode\LaravelNotionApi\Entities\Collections\BlockCollection;
65
use FiveamCode\LaravelNotionApi\Exceptions\HandlingException;
76
use FiveamCode\LaravelNotionApi\Notion;
8-
use Illuminate\Support\Collection;
7+
use FiveamCode\LaravelNotionApi\Exceptions\NotionException;
8+
use FiveamCode\LaravelNotionApi\Entities\Collections\BlockCollection;
99

1010
class Block extends Endpoint
1111
{
@@ -24,39 +24,17 @@ public function __construct(Notion $notion, string $blockId)
2424
*
2525
* @return BlockCollection
2626
*/
27-
public function children(): Collection
28-
{
29-
return $this->collectChildren()->getResults();
30-
}
31-
32-
/**
33-
* Retrieve block children (as raw json-data)
34-
* url: https://api.notion.com/{version}/blocks/{block_id}/children
35-
* notion-api-docs: https://developers.notion.com/reference/get-block-children
36-
*
37-
* @return array
38-
*/
39-
public function childrenRaw(): array
40-
{
41-
return $this->collectChildren()->getRawResults();
42-
}
43-
44-
private function collectChildren(): BlockCollection
27+
public function children(): BlockCollection
4528
{
4629
$response = $this->get(
4730
$this->url(Endpoint::BLOCKS . "/" . $this->blockId . "/children" . "?{$this->buildPaginationQuery()}")
4831
);
49-
if (!$response->ok())
50-
throw HandlingException::instance("Block not found.", ["blockId" => $this->blockId]);
51-
5232

53-
$blockCollection = new BlockCollection($response->json());
54-
return $blockCollection;
33+
return new BlockCollection($response->json());
5534
}
5635

5736
public function create(): array
5837
{
59-
//toDo
60-
throw new \Exception("not implemented yet");
38+
throw new HandlingException("Not implemented");
6139
}
6240
}

src/Endpoints/Database.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ public function query(): PageCollection
4848
$this->url(Endpoint::DATABASES . "/{$this->databaseId}/query"),
4949
$postData
5050
)
51-
5251
->json();
5352

5453
return new PageCollection($response);

src/Endpoints/Databases.php

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@
33
namespace FiveamCode\LaravelNotionApi\Endpoints;
44

55
use FiveamCode\LaravelNotionApi\Entities\Database;
6-
use FiveamCode\LaravelNotionApi\Entities\Collections\DatabaseCollection;
76
use FiveamCode\LaravelNotionApi\Exceptions\HandlingException;
8-
use Illuminate\Support\Collection;
7+
use FiveamCode\LaravelNotionApi\Entities\Collections\DatabaseCollection;
98

109

1110
/**
@@ -18,38 +17,17 @@
1817
*/
1918
class Databases extends Endpoint implements EndpointInterface
2019
{
21-
22-
2320
/**
2421
* List databases
2522
* url: https://api.notion.com/{version}/databases
2623
* notion-api-docs: https://developers.notion.com/reference/get-databases
2724
*
2825
* @return DatabaseCollection
2926
*/
30-
public function all(): Collection
31-
{
32-
return $this->collect()->getResults();
33-
}
34-
35-
/**
36-
* List databases (raw json-data)
37-
* url: https://api.notion.com/{version}/databases
38-
* notion-api-docs: https://developers.notion.com/reference/get-databases
39-
*
40-
* @return array
41-
*/
42-
public function allRaw(): array
43-
{
44-
return $this->collect()->getRawResults();
45-
}
46-
47-
// TODO rename this function - receive, access, fetch?
48-
private function collect(): DatabaseCollection
27+
public function all(): DatabaseCollection
4928
{
5029
$resultData = $this->getJson($this->url(Endpoint::DATABASES) . "?{$this->buildPaginationQuery()}");
51-
$databaseCollection = new DatabaseCollection($resultData);
52-
return $databaseCollection;
30+
return new DatabaseCollection($resultData);
5331
}
5432

5533
/**

src/Endpoints/Endpoint.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,13 @@ protected function get(string $url)
9090
*/
9191
protected function post(string $url, array $body)
9292
{
93-
return $this->notion->getConnection()->post($url, $body);
93+
$response = $this->notion->getConnection()->post($url, $body);
94+
95+
if ($response->failed())
96+
throw NotionException::fromResponse($response);
97+
98+
$this->response = $response;
99+
return $response;
94100
}
95101

96102

src/Endpoints/Pages.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use FiveamCode\LaravelNotionApi\Entities\Page;
66
use FiveamCode\LaravelNotionApi\Exceptions\HandlingException;
7+
use FiveamCode\LaravelNotionApi\Exceptions\NotionException;
78

89
class Pages extends Endpoint implements EndpointInterface
910
{
@@ -22,20 +23,19 @@ public function find(string $pageId): Page
2223
$this->url(Endpoint::PAGES . "/" . $pageId)
2324
);
2425

25-
if(!$response->ok())
26-
throw HandlingException::instance("Page not found.", ["pageId" => $pageId]);
27-
2826
return new Page($response->json());
2927
}
3028

31-
public function create(): array{
29+
public function create(): array
30+
{
3231
//toDo
33-
throw new \Exception("not implemented yet");
32+
throw new HandlingException("Not implemented");
3433
}
3534

3635

37-
public function updateProperties(): array{
36+
public function updateProperties(): array
37+
{
3838
//toDo
39-
throw new \Exception("not implemented yet");
39+
throw new HandlingException("Not implemented");
4040
}
4141
}

src/Endpoints/Users.php

Lines changed: 4 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -15,32 +15,13 @@ class Users extends Endpoint implements EndpointInterface
1515
* url: https://api.notion.com/{version}/users
1616
* notion-api-docs: https://developers.notion.com/reference/get-users
1717
*
18-
* @return Collection
18+
* @return UserCollection
1919
*/
20-
public function all(): Collection
20+
public function all(): UserCollection
2121
{
22-
return $this->collect()->getResults();
23-
}
24-
25-
26-
/**
27-
* List users (raw json-data)
28-
* url: https://api.notion.com/{version}/users
29-
* notion-api-docs: https://developers.notion.com/reference/get-users
30-
*
31-
* @return array
32-
*/
33-
public function allRaw(): array
34-
{
35-
return $this->collect()->getRawResults();
36-
}
37-
38-
private function collect(): UserCollection{
39-
$result = $this->get(
40-
$this->url(Endpoint::USERS . "?{$this->buildPaginationQuery()}")
41-
);
22+
$resultData = $this->getJson($this->url(Endpoint::USERS) . "?{$this->buildPaginationQuery()}");
4223

43-
return new UserCollection($result->json());
24+
return new UserCollection($resultData);
4425
}
4526

4627
/**
@@ -57,10 +38,6 @@ public function find(string $userId): User
5738
$this->url(Endpoint::USERS . "/" . $userId)
5839
);
5940

60-
if (!$response->ok())
61-
throw HandlingException::instance("User not found.", ["userId" => $userId]);
62-
63-
6441
return new User($response->json());
6542
}
6643
}

src/Entities/Blocks/Block.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
namespace FiveamCode\LaravelNotionApi\Entities\Blocks;
44

55
use DateTime;
6+
use Illuminate\Support\Arr;
67
use FiveamCode\LaravelNotionApi\Entities\Entity;
78
use FiveamCode\LaravelNotionApi\Exceptions\HandlingException;
8-
use Illuminate\Support\Arr;
99

1010
class Block extends Entity
1111
{

src/Entities/Collections/BlockCollection.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
namespace FiveamCode\LaravelNotionApi\Entities\Collections;
44

5-
use FiveamCode\LaravelNotionApi\Entities\Blocks\Block;
65
use Illuminate\Support\Collection;
6+
use FiveamCode\LaravelNotionApi\Entities\Blocks\Block;
77

88

99
class BlockCollection extends EntityCollection

0 commit comments

Comments
 (0)