<commit_before><?php

namespace Wallhaven;

use DateTime;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
use PHPHtmlParser\Dom;
use Wallhaven\Exceptions\DownloadException;
use Wallhaven\Exceptions\LoginException;
use Wallhaven\Exceptions\NotFoundException;
use Wallhaven\Exceptions\ParseException;

/**
 * Wallpaper
 *
 * @package Wallhaven
 */
class Wallpaper
{
    /**
     * @var Client
     */
    private $client;

    /**
     * @var bool Cache enabled.
     */
    private $cacheEnabled;

    /**
     * @var Dom Cached DOM.
     */
    private $dom;

    /**
     * @var int Wallpaper ID.
     */
    private $id;

    private $tags;
    private $purity;
    private $resolution;
    private $size;
    private $category;
    private $views;
    private $favorites;
    private $featuredBy;
    private $featuredDate;
    private $uploadedBy;
    private $uploadedDate;

    private $imageUrl;


    /**
     * @param int    $id     Wallpaper's ID.
     * @param Client $client HTTP Client.
     */
    public function __construct($id, Client $client)
    {
        $this->id = $id;
        $this->client = $client;
        $this->cacheEnabled = true;
    }

    /**
     * @return int Wallpaper ID.
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * @param array $properties Properties.
     */
    public function setProperties(array $properties)
    {
        foreach ($properties as $key => $value) {
            $this->$key = $value;
        }
    }

    /**
     * Enable or disable caching of wallpaper information. It's recommended to leave this enabled (default) unless
     * you really need real-time information. If you disable caching, performance will be severely degraded.
     *
     * @param bool $enabled Whether caching should be enabled.
     */
    public function setCacheEnabled($enabled)
    {
        $this->cacheEnabled = $enabled;
    }

    /**
     * Get wallpaper tags.
     *
     * @return string[] Tags.
     */
    public function getTags()
    {
        if ($this->cacheEnabled && $this->tags !== null) {
            return $this->tags;
        }

        $dom = $this->getDom();

        $this->tags = [];

        foreach ($dom->find('a.tagname') as $e) {
            $this->tags[] = $e->text;
        }

        return $this->tags;
    }

    /**
     * @return Dom
     * @throws LoginException  Thrown if access to the wallpaper was denied.
     * @throws NotFoundException Thrown if the wallpaper was not found.
     */
    private function getDom()
    {
        if ($this->cacheEnabled && $this->dom !== null) {
            return $this->dom;
        }

        try {
            $response = $this->client->get(Wallhaven::URL_WALLPAPER . '/' . $this->id)->getBody()->getContents();
        } catch (RequestException $e) {
            $code = $e->getResponse()->getStatusCode();
            if ($code == 403) {
                throw new LoginException("Access to wallpaper is forbidden.");
            } else if ($code == 404) {
                throw new NotFoundException("Wallpaper not found.");
            } else {
                throw $e;
            }
        }

        $dom = new Dom();
        $dom->load($response);

        $this->dom = $this->cacheEnabled ? $dom : null;

        return $dom;
    }

    /**
     * @return int Purity.
     */
    public function getPurity()
    {
        if ($this->cacheEnabled && $this->purity !== null) {
            return $this->purity;
        }<commit_msg>Fix getStatusCode sometimes failing<commit_after><?php

namespace Wallhaven;

use DateTime;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
use PHPHtmlParser\Dom;
use Wallhaven\Exceptions\DownloadException;
use Wallhaven\Exceptions\LoginException;
use Wallhaven\Exceptions\NotFoundException;
use Wallhaven\Exceptions\ParseException;

/**
 * Wallpaper
 *
 * @package Wallhaven
 */
class Wallpaper
{
    /**
     * @var Client
     */
    private $client;

    /**
     * @var bool Cache enabled.
     */
    private $cacheEnabled;

    /**
     * @var Dom Cached DOM.
     */
    private $dom;

    /**
     * @var int Wallpaper ID.
     */
    private $id;

    private $tags;
    private $purity;
    private $resolution;
    private $size;
    private $category;
    private $views;
    private $favorites;
    private $featuredBy;
    private $featuredDate;
    private $uploadedBy;
    private $uploadedDate;

    private $imageUrl;


    /**
     * @param int    $id     Wallpaper's ID.
     * @param Client $client HTTP Client.
     */
    public function __construct($id, Client $client)
    {
        $this->id = $id;
        $this->client = $client;
        $this->cacheEnabled = true;
    }

    /**
     * @return int Wallpaper ID.
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * @param array $properties Properties.
     */
    public function setProperties(array $properties)
    {
        foreach ($properties as $key => $value) {
            $this->$key = $value;
        }
    }

    /**
     * Enable or disable caching of wallpaper information. It's recommended to leave this enabled (default) unless
     * you really need real-time information. If you disable caching, performance will be severely degraded.
     *
     * @param bool $enabled Whether caching should be enabled.
     */
    public function setCacheEnabled($enabled)
    {
        $this->cacheEnabled = $enabled;
    }

    /**
     * Get wallpaper tags.
     *
     * @return string[] Tags.
     */
    public function getTags()
    {
        if ($this->cacheEnabled && $this->tags !== null) {
            return $this->tags;
        }

        $dom = $this->getDom();

        $this->tags = [];

        foreach ($dom->find('a.tagname') as $e) {
            $this->tags[] = $e->text;
        }

        return $this->tags;
    }

    /**
     * @return Dom
     * @throws LoginException  Thrown if access to the wallpaper was denied.
     * @throws NotFoundException Thrown if the wallpaper was not found.
     */
    private function getDom()
    {
        if ($this->cacheEnabled && $this->dom !== null) {
            return $this->dom;
        }

        try {
            $response = $this->client->get(Wallhaven::URL_WALLPAPER . '/' . $this->id)->getBody()->getContents();
        } catch (RequestException $e) {
            $code = $e->getCode();
            if ($code == 403) {
                throw new LoginException("Access to wallpaper is forbidden.");
            } else if ($code == 404) {
                throw new NotFoundException("Wallpaper not found.");
            } else {
                throw $e;
            }
        }

        $dom = new Dom();
        $dom->load($response);

        $this->dom = $this->cacheEnabled ? $dom : null;

        return $dom;
    }

    /**
     * @return int Purity.
     */
    public function getPurity()
    {
        if ($this->cacheEnabled && $this->purity !== null) {
            return $this->purity;
        }
{'fraction_non_alphanumeric': 0.13016910615319027, 'fraction_numerical': 0.0017052721330112265, 'mean_word_length': 1.8903490759753594, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 66, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6510941', 'n_tokens_mistral': 2100, 'n_tokens_neox': 2010, 'n_words': 535}