Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,16 +129,21 @@ $client->all(array('type' => 'library'));
$client->all(array('vendor' => 'sylius'));
```

#### Custom Packagist Repositories
#### Custom Packagist repositories

You can also set a custom Packagist Repository URL:
You can also set a custom Packagist repository URL:

```php
<?php

$client->setPackagistUrl('https://custom.packagist.site.org');
```

## Errors

* A `Packagist\Api\PackageNotFoundException` will be thrown when the Packagist API returns a 404 response.
* An `\InvalidArgumentException` will be thrown when the respond from Packagist was not able to be parsed.

## License

`packagist-api` is licensed under the MIT License - see the LICENSE file for details.
Expand Down
16 changes: 16 additions & 0 deletions spec/Packagist/Api/ClientSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@
namespace spec\Packagist\Api;

use GuzzleHttp\Client as HttpClient;
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Response;
use GuzzleHttp\Psr7\Stream;
use Packagist\Api\Client;
use Packagist\Api\PackageNotFoundException;
use Packagist\Api\Result\Factory;
use PhpSpec\ObjectBehavior;

Expand Down Expand Up @@ -195,4 +198,17 @@ public function it_filters_package_names_by_vendor(HttpClient $client, Factory $
$this->all(['vendor' => 'sylius']);
}

public function it_throws_exception_on_404s(HttpClient $client): void
{
$request = new Request('GET', 'https://packagist.org/packages/i-do/not-exist.json');
$response = new Response(404, [], json_encode(['status' => 'error', 'message' => 'Package not found']));
$exception = new ClientException('', $request, $response);

$client->request('GET', 'https://packagist.org/packages/i-do/not-exist.json')
->shouldBeCalled()
->willThrow($exception);

$this->shouldThrow(PackageNotFoundException::class)
->during('get', ['i-do/not-exist']);
}
}
8 changes: 6 additions & 2 deletions src/Packagist/Api/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use GuzzleHttp\Exception\GuzzleException;
use Packagist\Api\Result\Factory;
use Packagist\Api\Result\Package;
use Psr\Http\Message\StreamInterface;

/**
* Packagist Api
Expand Down Expand Up @@ -247,6 +246,8 @@ protected function multiRespond(string $url1, string $url2)
*
* @param string $url
* @return string
* @throws PackageNotFoundException
* @throws GuzzleException
*/
protected function request(string $url): string
{
Expand All @@ -256,7 +257,10 @@ protected function request(string $url): string
->getBody()
->getContents();
} catch (GuzzleException $e) {
return json_encode([]);
if ($e->getCode() === 404) {
throw new PackageNotFoundException('The requested package was not found.', 404);
}
throw $e;
}
}

Expand Down
14 changes: 14 additions & 0 deletions src/Packagist/Api/PackageNotFoundException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

namespace Packagist\Api;

use InvalidArgumentException;

/**
* Thrown when a requested package was not found in the Packagist API.
*/
class PackageNotFoundException extends InvalidArgumentException
{
}
8 changes: 4 additions & 4 deletions src/Packagist/Api/Result/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,15 @@ public function createSearchResults(array $results): array
* @param array $packages
* @return Package[]
*/
public function createComposerPackagesResults(array $packages)
public function createComposerPackagesResults(array $packages): array
{
$created = array();
$created = [];

foreach ($packages as $name => $package) {
// Create an empty package, only contains versions
$createdPackage = array(
$createdPackage = [
'versions' => [],
);
];
foreach ($package as $branch => $version) {
$createdPackage['versions'][$branch] = $version;
}
Expand Down