<?php

namespace App\Integrations\MozApi;


class Client
{
    /**
     * @var HttpClient
     */
    private $clientFactory;
    /**
     * @var string
     */
    private $accessId;
    /**
     * @var string
     */
    private $secret;

    public function __construct(HttpClientFactory $clientFactory, string $accessId, string $secret)
    {
        $this->clientFactory = $clientFactory;
        $this->accessId = $accessId;
        $this->secret = $secret;
    }

    public function getUsedRows(): int
    {
        $response = $this->clientFactory->create()->request('POST', 'https://lsapi.seomoz.com/v2/usage_data', [
            'auth' => [$this->accessId, $this->secret],
            'body' => "{}"
        ]);
        return json_decode((string)$response->getBody())->rows_consumed;
    }

    public function getDomainAuthority(string $url): int
    {
        $response = $this->clientFactory->create()->request('POST', 'https://lsapi.seomoz.com/v2/url_metrics', [
            'auth' => ['mozscape-9b46e99efb', 'ed054868472da9ea545f8c2466af2fa0'],
            'json' => ['targets' => [$url]]
        ]);

        return json_decode((string)$response->getBody())->results[0]->domain_authority;
    }
}