<commit_before><?php
/**
 * (c) 2012 Vespolina Project http://www.vespolina-project.org
 *
 * This source file is subject to the MIT license that is bundled
 * with this source code in the file LICENSE.
 */

namespace Vespolina\Invoice\Manager;

use Vespolina\Entity\Billing\BillingRequest;
use Vespolina\Entity\Invoice\InvoiceInterface;
use Vespolina\Entity\Partner\PartnerInterface;
use Vespolina\Invoice\Gateway\InvoiceGateway;

/**
 * @author <NAME> <<EMAIL>>
 */
class InvoiceManager implements InvoiceManagerInterface
{
    private $invoiceClass;
    protected $gateway;

    public function __construct(InvoiceGateway $gateway, $invoiceClass)
    {
        if (!class_exists($invoiceClass)) {
            throw new InvalidConfigurationException(sprintf("Class '%s' not found", $invoiceClass));
        }

        $this->gateway = $gateway;
        $this->invoiceClass = $invoiceClass;
    }

    /**
     * @inheritdoc
     */
    public function createInvoice()
    {
        $invoice = new $this->invoiceClass();

        return $invoice;
    }

    /**
     * @inheritdoc
     */
    public function findById($id)
    {
        return $this->gateway->createQuery('Select')
            ->filterEqual('id', $id)
            ->one()
        ;
    }

    /**
     * @inheritdoc
     */
    public function findAll()
    {
        $query = $this->gateway->createQuery('Select');
        return $query->sort('paid', 'asc')
            ->all()
            ;
    }

    /**
     * @inheritdoc
     */
    public function findAllInvoicesByPartner(PartnerInterface $partner)
    {
        $query = $this->gateway->createQuery('Select');
        return $query->filterEqual('partner', $partner)
            ->sort('periodEnd', 'desc')
            ->all()
        ;
    }

    /**
     * @inheritdoc
     */
    public function findLastInvoiceByPartner(PartnerInterface $partner)
    {
        $query = $this->gateway->createQuery('Select');
        return $query->filterEqual('partner', $partner)
            ->sort('periodEnd', 'desc')
            ->one()
            ;
    }

    /**
     * @inheritdoc
     */
    public function findInvoicesByPartnerAndPeriod(PartnerInterface $partner, $periodStart, $periodEnd)
    {
        $query = $this->gateway->createQuery('Select');
        return $query->filterEqual('partner', $partner)
            ->filterEqual('periodStart', $periodStart)
            ->filterEqual('periodEnd', $periodEnd)<commit_msg>updates for invoice manager, added method for getting invoices using interval<commit_after><?php
/**
 * (c) 2012 Vespolina Project http://www.vespolina-project.org
 *
 * This source file is subject to the MIT license that is bundled
 * with this source code in the file LICENSE.
 */

namespace Vespolina\Invoice\Manager;

use Molino\Doctrine\ORM\SelectQuery;
use Vespolina\Entity\Billing\BillingRequest;
use Vespolina\Entity\Invoice\InvoiceInterface;
use Vespolina\Entity\Partner\PartnerInterface;
use Vespolina\Invoice\Gateway\InvoiceGateway;

/**
 * @author <NAME> <<EMAIL>>
 */
class InvoiceManager implements InvoiceManagerInterface
{
    private $invoiceClass;
    protected $gateway;

    public function __construct(InvoiceGateway $gateway, $invoiceClass)
    {
        if (!class_exists($invoiceClass)) {
            throw new InvalidConfigurationException(sprintf("Class '%s' not found", $invoiceClass));
        }

        $this->gateway = $gateway;
        $this->invoiceClass = $invoiceClass;
    }

    /**
     * @inheritdoc
     */
    public function createInvoice()
    {
        $invoice = new $this->invoiceClass();

        return $invoice;
    }

    /**
     * @inheritdoc
     */
    public function findById($id)
    {
        return $this->gateway->createQuery('Select')
            ->filterEqual('id', $id)
            ->one()
        ;
    }

    /**
     * @inheritdoc
     */
    public function findAll()
    {
        $query = $this->gateway->createQuery('Select');
        return $query->sort('paid', 'asc')
            ->all()
            ;
    }

    /**
     * @inheritdoc
     */
    public function findAllInvoicesByPartner(PartnerInterface $partner)
    {
        $query = $this->gateway->createQuery('Select')
            ->filterEqual('partner', $partner)
            ->sort('periodEnd', 'desc');

        return $query->all();
    }

    /**
     * @param PartnerInterface $partner
     * @param null $interval
     * @return mixed
     */
    public function findAllInvoicesByPartnerUsingInterval(PartnerInterface $partner, $interval = null)
    {
        /** @var $query SelectQuery */
        $query = $this->gateway->createQuery('Select');

        $query
            ->filterEqual('partner', $partner)
            ->filterGreaterEqual('issuedDate', new \DateTime($interval))
            ->sort('periodEnd', 'desc')
        ;

        return $query->all();
    }

    /**
     * @inheritdoc
     */
    public function findLastInvoiceByPartner(PartnerInterface $partner)
    {
        $query = $this->gateway->createQuery('Select');
        return $query->filterEqual('partner', $partner)
            ->sort('periodEnd', 'desc')
            ->one()
            ;
    }

    /**
     * @inheritdoc
     */
    public function findInvoicesByPartnerAndPeriod(PartnerInterface $partner, $periodStart, $periodEnd)
    {
        $query = $this->gateway->createQuery('Select');
        return $query->filterEqual('partner', $partner)
            ->filterEqual('periodStart', $periodStart)
            ->filterEqual('periodEnd', $periodEnd)
{'fraction_non_alphanumeric': 0.13696399493396055, 'fraction_numerical': 0.0014474398407816175, 'mean_word_length': 2.4186765615337045, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 67, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '2395186', 'n_tokens_mistral': 1644, 'n_tokens_neox': 1601, 'n_words': 310}