<commit_before><?php

/*
 * This file is part of the DigiDoc package.
 *
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace KG\DigiDoc;

use KG\DigiDoc\Exception\ApiException;
use KG\DigiDoc\Exception\RuntimeException;
use KG\DigiDoc\Soap\Wsdl\SignedDocInfo;

class Api implements ApiInterface
{
    const CONTENT_TYPE_HASHCODE = 'HASHCODE';
    const CONTENT_TYPE_EMBEDDED = 'EMBEDDED_BASE64';

    const DOC_FORMAT = 'BDOC';
    const DOC_VERSION = '2.1';

    /**
     * @var \SoapClient
     */
    private $client;

    /**
     * @var Encoder
     */
    private $encoder;

    /**
     * @var Tracker
     */
    private $tracker;

    /**
     * @param \SoapClient  $client
     * @param Encoder|null $encoder
     * @param Tracker|null $tracker
     */
    public function __construct(\SoapClient $client, Encoder $encoder = null, Tracker $tracker = null)
    {
        $this->client = $client;
        $this->encoder = $encoder ?: new Encoder();
        $this->tracker = $tracker ?: new Tracker();
    }

    /**
     * {@inheritDoc}
     */
    public function create()
    {
        $result = $this->call('startSession', array('', '', true, ''));
        $result = $this->call('createSignedDoc', array($sessionId = $result['Sesscode'], self::DOC_FORMAT, self::DOC_VERSION));

        $container = new Container(new Session($sessionId));

        $this->tracker->add($container);

        return $container;
    }

    /**
     * {@inheritDoc}
     */
    public function open($path)
    {
        $result = $this->call('startSession', array('', $this->encoder->encodeFileContent($path), true, ''));

        $container = new Container(
            new Session($result['Sesscode']),
            $this->createAndTrack($result['SignedDocInfo']->DataFileInfo, 'KG\DigiDoc\File'),
            $this->createAndTrack($result['SignedDocInfo']->SignatureInfo, 'KG\DigiDoc\Signature')
        );

        $this->tracker->add($container);

        return $container;
    }

    /**
     * {@inheritDoc}
     */
    public function close(Container $container)
    {
        $this->call('closeSession', array($container->getSession()->getId()));
    }

    /**
     * {@inheritDoc}
     */
    public function update(Container $container)
    {
        $this->failIfNotMerged($container);

        $session = $container->getSession();
        $tracker = $this->tracker;

        $this
            ->addFiles($session, $container->getFiles())
            ->addSignatures($session, $container->getSignatures())
            ->sealSignatures($session, $container->getSignatures())
        ;
    }

    /**
     * {@inheritDoc}
     */
    public function write(Container $container, $path)
    {
        $this->failIfNotMerged($container);

        $result = $this->call('getSignedDoc', array($container->getSession()->getId()));

        file_put_contents($path, $this->encoder->decode($result['SignedDocData']));
    }

    /**
     * {@inheritDoc}
     */
    public function merge(Container $container)
    {
        if ($this->tracker->has($container)) {
            return;
        }

        $this->tracker->add($container);
        $this->tracker->add($container->getFiles()->toArray());
        $this->tracker->add($container->getSignatures()->toArray());
    }

    private function addFiles(Session $session, $files)
    {
        foreach ($files as $file) {
            // Skips already tracked files, because they're already added.
            if ($tracker->has($file)) {
                continue;
            }

            $this->call('addDataFile', array(
                $session->getId(),
                $file->getName(),
                $file->getMimeType(),
                self::CONTENT_TYPE_EMBEDDED,
                $file->getSize(),
                '',
                '',
                $this->encoder->encodeFileContent($file->getPathname()),
            ));

            $this->tracker->add($file);
        }

        return $this;
    }

    private function addSignatures(Session $session, $signatures)
    {
        foreach ($signatures as $signature) {
            // Skips already tracked signatures, because they're already added.
            if ($tracker->has($signature)) {
                continue;
            }

            $result = $this->call('prepareSignature', array($session->getId(), $signature->getCertSignature(), $signature->getCertId()));

            $signature->setId($result['SignatureId']);
            $signature->setChallenge($result['SignedInfoDigest']);
<commit_msg>Fix undefined variable use in Api<commit_after><?php

/*
 * This file is part of the DigiDoc package.
 *
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace KG\DigiDoc;

use KG\DigiDoc\Exception\ApiException;
use KG\DigiDoc\Exception\RuntimeException;
use KG\DigiDoc\Soap\Wsdl\SignedDocInfo;

class Api implements ApiInterface
{
    const CONTENT_TYPE_HASHCODE = 'HASHCODE';
    const CONTENT_TYPE_EMBEDDED = 'EMBEDDED_BASE64';

    const DOC_FORMAT = 'BDOC';
    const DOC_VERSION = '2.1';

    /**
     * @var \SoapClient
     */
    private $client;

    /**
     * @var Encoder
     */
    private $encoder;

    /**
     * @var Tracker
     */
    private $tracker;

    /**
     * @param \SoapClient  $client
     * @param Encoder|null $encoder
     * @param Tracker|null $tracker
     */
    public function __construct(\SoapClient $client, Encoder $encoder = null, Tracker $tracker = null)
    {
        $this->client = $client;
        $this->encoder = $encoder ?: new Encoder();
        $this->tracker = $tracker ?: new Tracker();
    }

    /**
     * {@inheritDoc}
     */
    public function create()
    {
        $result = $this->call('startSession', array('', '', true, ''));
        $result = $this->call('createSignedDoc', array($sessionId = $result['Sesscode'], self::DOC_FORMAT, self::DOC_VERSION));

        $container = new Container(new Session($sessionId));

        $this->tracker->add($container);

        return $container;
    }

    /**
     * {@inheritDoc}
     */
    public function open($path)
    {
        $result = $this->call('startSession', array('', $this->encoder->encodeFileContent($path), true, ''));

        $container = new Container(
            new Session($result['Sesscode']),
            $this->createAndTrack($result['SignedDocInfo']->DataFileInfo, 'KG\DigiDoc\File'),
            $this->createAndTrack($result['SignedDocInfo']->SignatureInfo, 'KG\DigiDoc\Signature')
        );

        $this->tracker->add($container);

        return $container;
    }

    /**
     * {@inheritDoc}
     */
    public function close(Container $container)
    {
        $this->call('closeSession', array($container->getSession()->getId()));
    }

    /**
     * {@inheritDoc}
     */
    public function update(Container $container)
    {
        $this->failIfNotMerged($container);

        $session = $container->getSession();

        $this
            ->addFiles($session, $container->getFiles())
            ->addSignatures($session, $container->getSignatures())
            ->sealSignatures($session, $container->getSignatures())
        ;
    }

    /**
     * {@inheritDoc}
     */
    public function write(Container $container, $path)
    {
        $this->failIfNotMerged($container);

        $result = $this->call('getSignedDoc', array($container->getSession()->getId()));

        file_put_contents($path, $this->encoder->decode($result['SignedDocData']));
    }

    /**
     * {@inheritDoc}
     */
    public function merge(Container $container)
    {
        if ($this->tracker->has($container)) {
            return;
        }

        $this->tracker->add($container);
        $this->tracker->add($container->getFiles()->toArray());
        $this->tracker->add($container->getSignatures()->toArray());
    }

    private function addFiles(Session $session, $files)
    {
        foreach ($files as $file) {
            // Skips already tracked files, because they're already added.
            if ($this->tracker->has($file)) {
                continue;
            }

            $this->call('addDataFile', array(
                $session->getId(),
                $file->getName(),
                $file->getMimeType(),
                self::CONTENT_TYPE_EMBEDDED,
                $file->getSize(),
                '',
                '',
                $this->encoder->encodeFileContent($file->getPathname()),
            ));

            $this->tracker->add($file);
        }

        return $this;
    }

    private function addSignatures(Session $session, $signatures)
    {
        foreach ($signatures as $signature) {
            // Skips already tracked signatures, because they're already added.
            if ($this->tracker->has($signature)) {
                continue;
            }

            $result = $this->call('prepareSignature', array($session->getId(), $signature->getCertSignature(), $signature->getCertId()));

            $signature->setId($result['SignatureId']);
            $signature->setChallenge($result['SignedInfoDigest']);

{'fraction_non_alphanumeric': 0.1583090693410978, 'fraction_numerical': 0.0008627197239296883, 'mean_word_length': 2.28632175761871, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 138, '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': '445184', 'n_tokens_mistral': 2726, 'n_tokens_neox': 2638, 'n_words': 489}