<commit_before><?php

namespace MediaLab\Shipping\Calculator;

use Sylius\Component\Addressing\Model\AddressInterface;
use Sylius\Component\Shipping\Model\ShippableInterface;
use DateTime;
use MediaLab\Shipping\Model\Estimation;
use MediaLab\Shipping\Model\Cost;
use MediaLab\Shipping\SFExpress\Client;

class SFExpressCalculator implements CalculatorInterface
{
    private $client;

    public function __construct(Client $client = null)
    {
        if (null === $client) {
            $client = new Client();
        }

        $this->client = $client;
    }

    public function calculate(AddressInterface $origin, AddressInterface $destination, ShippableInterface $shippable)
    {
        $estimations = [];

        foreach($this->client->getRates(
            $this->getRegionCode($origin),
            $this->getRegionCode($destination),
            $shippable->getShippingWeight(),
            $shippable->getShippingWidth() * $shippable->getShippingHeight() * $shippable->getShippingDepth()
        ) as $rate) {
            $estimations[] = (new Estimation())
                ->setCarrier('S.F. Express')
                ->setServiceName($rate['cargoTypeName'])
                ->setServiceCode($rate['cargoTypeCode'])
                ->setDeliveryDate(null === $rate['deliverTime'] ? null : new DateTime($rate['deliverTime']))
                ->setCost((new Cost())
                    ->setCurrency($rate['currencyName'])
                    ->setAmount($rate['freight'])
                )
            ;
        }

        return $estimations;
    }

    private function getRegionCode(AddressInterface $address)
    {
        $provinceName = null === $address->getProvince() ? null : $address->getProvince()->getName();

        foreach ($this->getRegions() as $region) {
            if ($provinceName === $region['name']) {
                return $region['code'];
            }
        }

        throw new CalculatorException(sprintf(
            'Region %s, %s is not supported.',
            $provinceName,
            $address->getCountry()->getName()
        ));
    }

    private function getRegions()
    {<commit_msg>Better service name for SFExpress.<commit_after><?php

namespace MediaLab\Shipping\Calculator;

use Sylius\Component\Addressing\Model\AddressInterface;
use Sylius\Component\Shipping\Model\ShippableInterface;
use DateTime;
use MediaLab\Shipping\Model\Estimation;
use MediaLab\Shipping\Model\Cost;
use MediaLab\Shipping\SFExpress\Client;

class SFExpressCalculator implements CalculatorInterface
{
    private $client;

    public function __construct(Client $client = null)
    {
        if (null === $client) {
            $client = new Client();
        }

        $this->client = $client;
    }

    public function calculate(AddressInterface $origin, AddressInterface $destination, ShippableInterface $shippable)
    {
        $estimations = [];

        foreach($this->client->getRates(
            $this->getRegionCode($origin),
            $this->getRegionCode($destination),
            $shippable->getShippingWeight(),
            $shippable->getShippingWidth() * $shippable->getShippingHeight() * $shippable->getShippingDepth()
        ) as $rate) {
            $estimations[] = (new Estimation())
                ->setCarrier('S.F. Express')
                ->setServiceName($this->getServiceName($rate))
                ->setServiceCode($rate['limitTypeName'].' '.$rate['cargoTypeCode'])
                ->setDeliveryDate(null === $rate['deliverTime'] ? null : new DateTime($rate['deliverTime']))
                ->setCost((new Cost())
                    ->setCurrency($rate['currencyName'])
                    ->setAmount($rate['freight'])
                )
            ;
        }

        return $estimations;
    }
    
    private function getServiceName($rate)
    {
        $limitType = ucwords($rate['limitTypeName']);
        $cargoType = '';
        
        switch ($rate['cargoTypeCode']) {
            case 'C201': 
                $cargoType = ' (Package)';
            break;
            case 'C1':
                $cargoType = ' (File)';
            break;
        }
        
        return "$limitType$cargoType";
    }

    private function getRegionCode(AddressInterface $address)
    {
        $provinceName = null === $address->getProvince() ? null : $address->getProvince()->getName();

        foreach ($this->getRegions() as $region) {
            if ($provinceName === $region['name']) {
                return $region['code'];
            }
        }

        throw new CalculatorException(sprintf(
            'Region %s, %s is not supported.',
            $provinceName,
            $address->getCountry()->getName()
        ));
    }

    private function getRegions()
    {
{'fraction_non_alphanumeric': 0.13508403361344537, 'fraction_numerical': 0.0008403361344537816, 'mean_word_length': 2.214719783929777, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 48, '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': '253210', 'n_tokens_mistral': 1330, 'n_tokens_neox': 1276, 'n_words': 229}