<commit_before><?php

namespace Biblys\Data;

use GuzzleHttp\Client as Http;
use Biblys\Isbn\Isbn as Isbn;

class Client
{
    public function __construct($options = [])
    {
        $this->server = "http://data.biblys.fr";
        if (isset($options["server"])) {
            $this->server = $options["server"];
        }

        $this->apiKey = "";
        if (isset($options["apiKey"])) {
            $this->apiKey = $options["apiKey"];
        }

        $this->http = new Http([
            'base_uri' => $this->server,
            'headers' => [
                'Authorization' => $this->apiKey
            ],
            'http_errors' => false
        ]);
        if (isset($options["http"])) {
            $this->http = $options["http"];
        }
    }

    public function push(Book $book)
    {
        return $this->pushBook($book);
    }

    public function pushBook(Book $book)
    {
        // Try to fetch the book from the server
        $fetch = $this->getBook($book->getEan());

        // If it doesn't exist, create it
        if (!$fetch) {
            $this->createBook($book);
            return;
        }

        // Else, update it (to be implemented server-side)
        return;
    }

    public function createBook(Book $book)
    {
        $response = $this->http->request('POST', '/api/v0/books/', [
            'form_params' => [
                'ean' => $book->getEan(),
                'title' => $book->getTitle()
            ]
        ]);
        $status = $response->getStatusCode();

        if ($status !== 201) {
            throw new \Exception("Server answered $status");
        }

        return true;
    }

    public function getBook($ean)
    {
        // Check that EAN is valid
        $isbn = new Isbn($ean);
        if (!$isbn->isValid()) {
            throw new \Exception("$ean is not a valid ISBN");
        }
        $ean = $isbn->format('EAN');

        // Fetch book from server with this ISBN
        $response = $this->http->request('GET', "/api/v0/books/$ean");
        $status = $response->getStatusCode();

        // Return false if the book does not exist
        if ($status === 404) {
            return false;
        }

        // Return the book if it exists
        if ($status === 200) {
            $body = (string) $response->getBody();
            $bookData = json_decode($body);

            $book = new Book();
            $book->setEan($bookData->ean);
            $book->setTitle($bookData->title);
            return $book;
        }

        // Else, throw an exception
        throw new \Exception("Server answered $status");
    }

    public function pushPublisher(Publisher $publisher)
    {
        // Try to fetch the book from the server
        $fetch = $this->getPublisher($publisher->getId());

        // If it doesn't exist, create it
        if (!$fetch) {
            return $this->createPublisher($publisher);
        }

        // Else, update it (to be implemented server-side)
        return;
    }

    public function createPublisher(Publisher $publisher)
    {<commit_msg>Push Publisher before pushing a Book<commit_after><?php

namespace Biblys\Data;

use GuzzleHttp\Client as Http;
use Biblys\Isbn\Isbn as Isbn;

class Client
{
    public function __construct($options = [])
    {
        $this->server = "http://data.biblys.fr";
        if (isset($options["server"])) {
            $this->server = $options["server"];
        }

        $this->apiKey = "";
        if (isset($options["apiKey"])) {
            $this->apiKey = $options["apiKey"];
        }

        $this->http = new Http([
            'base_uri' => $this->server,
            'headers' => [
                'Authorization' => $this->apiKey
            ],
            'http_errors' => false
        ]);
        if (isset($options["http"])) {
            $this->http = $options["http"];
        }
    }

    public function push(Book $book)
    {
        return $this->pushBook($book);
    }

    public function pushBook(Book $book)
    {
        // Push related publisher
        $publisher = $book->getPublisher();
        $publisher = $this->pushPublisher($publisher);
        $book->setPublisher($publisher);

        // Try to fetch the book from the server
        $fetch = $this->getBook($book->getEan());

        // If it doesn't exist, create it
        if (!$fetch) {
            $this->createBook($book);
            return;
        }

        // Else, update it (to be implemented server-side)
        return;
    }

    public function createBook(Book $book)
    {
        $response = $this->http->request('POST', '/api/v0/books/', [
            'form_params' => [
                'ean' => $book->getEan(),
                'title' => $book->getTitle()
            ]
        ]);
        $status = $response->getStatusCode();

        if ($status !== 201) {
            throw new \Exception("Server answered $status");
        }

        return true;
    }

    public function getBook($ean)
    {
        // Check that EAN is valid
        $isbn = new Isbn($ean);
        if (!$isbn->isValid()) {
            throw new \Exception("$ean is not a valid ISBN");
        }
        $ean = $isbn->format('EAN');

        // Fetch book from server with this ISBN
        $response = $this->http->request('GET', "/api/v0/books/$ean");
        $status = $response->getStatusCode();

        // Return false if the book does not exist
        if ($status === 404) {
            return false;
        }

        // Return the book if it exists
        if ($status === 200) {
            $body = (string) $response->getBody();
            $bookData = json_decode($body);

            $book = new Book();
            $book->setEan($bookData->ean);
            $book->setTitle($bookData->title);
            return $book;
        }

        // Else, throw an exception
        throw new \Exception("Server answered $status");
    }

    public function pushPublisher(Publisher $publisher)
    {
        // Try to fetch the book from the server
        $fetch = $this->getPublisher($publisher->getId());

        // If it doesn't exist, create it
        if (!$fetch) {
            return $this->createPublisher($publisher);
        }

        // Else, update it (to be implemented server-side)
        return $fetch;
    }

    public function createPublisher(Publisher $publisher)
    {
{'fraction_non_alphanumeric': 0.14919037887124667, 'fraction_numerical': 0.003458575695645339, 'mean_word_length': 1.7072340425531916, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 80, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '893918', 'n_tokens_mistral': 1879, 'n_tokens_neox': 1778, 'n_words': 448}