<commit_before><?php


namespace Thatside\MoneybirdBundle\Services;

use Picqer\Financials\Moneybird\Connection;
use Picqer\Financials\Moneybird\Entities\Administration;
use Picqer\Financials\Moneybird\Entities\Contact;
use Picqer\Financials\Moneybird\Moneybird;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Thatside\MoneybirdBundle\Event\MoneybirdTokenEvent;
use Thatside\MoneybirdBundle\Model\SyncableContactInterface;

/**
 * Class ThatMoneybirdService
 * Provides wrapper for Moneybird PHP client by picqer
 */
class ThatMoneybirdService
{
    /** @var Connection  */
    protected $connection;
    /** @var  Moneybird */
    protected $moneybird = null;
    /** @var  CodeFetcherInterface */
    protected $codeFetcher;
    /** @var  EventDispatcherInterface */
    protected $eventDispatcher;
    /** @var  string|null */
    protected $authCode;
    /** @var  string */
    protected $token;

    /**
     * ThatMoneybirdService constructor.
     *
     * @param Connection $connection
     * @param CodeFetcherInterface $codeFetcher
     * @param EventDispatcherInterface $dispatcher
     */
    public function __construct(Connection $connection, CodeFetcherInterface $codeFetcher, EventDispatcherInterface $dispatcher)
    {
        $this->connection = $connection;
        $this->codeFetcher = $codeFetcher;
        $this->eventDispatcher = $dispatcher;

        $this->initMoneybird();
    }

    /**
     * Initialize Moneybird connection and main class, set up token and refresh it if needed
     *
     * @throws \Exception
     */
    public function initMoneybird()
    {
        if ($this->isMoneybirdEnabled()) {
            $this->updateAuthCode();
            $this->connection->setAuthorizationCode($this->authCode);

            $this->updateToken();
            $this->connection->setAccessToken($this->token);

            try {
                $this->connection->connect();
            } catch (\Exception $e) {
                throw new \Exception('Could not connect to Moneybird: ' . $e->getMessage());
            }

            $token = $this->connection->getAccessToken();
            $this->eventDispatcher->dispatch(MoneybirdTokenEvent::NAME, new MoneybirdTokenEvent($token));

            $this->connection->setAdministrationId($this->codeFetcher->getAdministrationId());

            $this->moneybird = new Moneybird($this->connection);
        }
    }

    /**
     * @return Moneybird
     */
    public function getMoneybird()
    {
        return $this->moneybird;
    }

    /**
     * Get Moneybird authorization URL from private method of Connection class
     *
     * @return string
     */
    public function getAuthUrl()
    {
        $connectionReflection = new \ReflectionClass($this->connection);
        $getAuthUrlMethod = $connectionReflection->getMethod('getAuthUrl');
        $getAuthUrlMethod->setAccessible(true);
        $authUrl = $getAuthUrlMethod->invoke($this->connection);

        return $authUrl;
    }

    /**
     * Check if moneybird is enabled for client (if auth code is not null)
     * @return bool
     */
    public function isMoneybirdEnabled()
    {
        return $this->codeFetcher->getAuthorizationCode() !== null;
    }

    /**
     * @param string $id
     */
    public function setAdministrationId($id)
    {
        $this->connection->setAdministrationId($id);
    }

    /**
     * @return Administration[]
     */
    public function getAdministrations()
    {
        return $this->moneybird->administration()->getAll();
    }

    /**
     * Create Moneybird contact
     * @param SyncableContactInterface $contact
     *
     * @return Contact
     */
    public function syncContact(SyncableContactInterface $contact)
    {
        $contactData = $contact->getMoneybirdContactData();
        $contactData->validate();

        return $this->moneybird->contact($contactData->attributes())->save();
    }

    /**
     * Update authorization code from code fetcher
     */
    private function updateAuthCode()
    {
        $this->authCode = $this->codeFetcher->getAuthorizationCode();
    }<commit_msg>Implemented basic client invoicing<commit_after><?php


namespace Thatside\MoneybirdBundle\Services;

use Picqer\Financials\Moneybird\Connection;
use Picqer\Financials\Moneybird\Entities\Administration;
use Picqer\Financials\Moneybird\Entities\Contact;
use Picqer\Financials\Moneybird\Moneybird;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Thatside\MoneybirdBundle\Event\MoneybirdTokenEvent;
use Thatside\MoneybirdBundle\Model\SyncableContactInterface;

/**
 * Class ThatMoneybirdService
 * Provides wrapper for Moneybird PHP client by picqer
 */
class ThatMoneybirdService
{
    /** @var Connection  */
    protected $connection;
    /** @var  Moneybird */
    protected $moneybird = null;
    /** @var  CodeFetcherInterface */
    protected $codeFetcher;
    /** @var  EventDispatcherInterface */
    protected $eventDispatcher;
    /** @var  string|null */
    protected $authCode;
    /** @var  string */
    protected $token;

    /**
     * ThatMoneybirdService constructor.
     *
     * @param Connection $connection
     * @param CodeFetcherInterface $codeFetcher
     * @param EventDispatcherInterface $dispatcher
     */
    public function __construct(Connection $connection, CodeFetcherInterface $codeFetcher, EventDispatcherInterface $dispatcher)
    {
        $this->connection = $connection;
        $this->codeFetcher = $codeFetcher;
        $this->eventDispatcher = $dispatcher;

        $this->initMoneybird();
    }

    /**
     * Initialize Moneybird connection and main class, set up token and refresh it if needed
     *
     * @throws \Exception
     */
    public function initMoneybird()
    {
        if ($this->isMoneybirdEnabled()) {
            $this->updateAuthCode();
            $this->connection->setAuthorizationCode($this->authCode);

            $this->updateToken();
            $this->connection->setAccessToken($this->token);

            try {
                $this->connection->connect();
            } catch (\Exception $e) {
                throw new \Exception('Could not connect to Moneybird: ' . $e->getMessage());
            }

            $token = $this->connection->getAccessToken();
            $this->eventDispatcher->dispatch(MoneybirdTokenEvent::NAME, new MoneybirdTokenEvent($token));

            $this->connection->setAdministrationId($this->codeFetcher->getAdministrationId());

            $this->moneybird = new Moneybird($this->connection);
        }
    }

    /**
     * @return Moneybird
     */
    public function getMoneybird()
    {
        return $this->moneybird;
    }

    /**
     * Get Moneybird authorization URL from private method of Connection class
     *
     * @return string
     */
    public function getAuthUrl()
    {
        $connectionReflection = new \ReflectionClass($this->connection);
        $getAuthUrlMethod = $connectionReflection->getMethod('getAuthUrl');
        $getAuthUrlMethod->setAccessible(true);
        $authUrl = $getAuthUrlMethod->invoke($this->connection);

        return $authUrl;
    }

    /**
     * Check if moneybird is enabled for client (if auth code is not null)
     * @return bool
     */
    public function isMoneybirdEnabled()
    {
        return $this->codeFetcher->getAuthorizationCode() !== null;
    }

    /**
     * @param string $id
     */
    public function setAdministrationId($id)
    {
        $this->connection->setAdministrationId($id);
    }

    /**
     * @return Administration[]
     */
    public function getAdministrations()
    {
        $administrationId = $this->codeFetcher->getAdministrationId();
        $this->connection->setAdministrationId(null);
        $administrations = $this->moneybird->administration()->getAll();
        $this->connection->setAdministrationId($administrationId);

        return $administrations;
    }

    /**
     * Create Moneybird contact
     * @param SyncableContactInterface $contact
     *
     * @return Contact
     */
    public function syncContact(SyncableContactInterface $contact)
    {
        $contactData = $contact->getMoneybirdContactData();
        $contactData->validate();

        return $this->moneybird->contact($contactData->attributes())->save();
    }

    /**
     * Update authorization code from code fetcher
     */
    private function updateAuthCode()
    {
        $this->authCode = $this->codeFetcher->getAuthorizationCode();
    }
{'fraction_non_alphanumeric': 0.11844705335348134, 'fraction_numerical': 0.0, 'mean_word_length': 2.758846657929227, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 105, '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': '3724728', 'n_tokens_mistral': 2380, 'n_tokens_neox': 2286, 'n_words': 457}