Skip to content

Commit

Permalink
Quote message (#23)
Browse files Browse the repository at this point in the history
* Add some meaning text to CXmlModelNotFoundException
* +CommentsTrait
*+QuoteMessage
* dont check for 8.2 for now :(
  • Loading branch information
mathielen authored Dec 6, 2023
1 parent 39e940d commit 6c21d10
Show file tree
Hide file tree
Showing 23 changed files with 7,806 additions and 66 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ jobs:
php-version: [
'7.4',
'8.0',
'8.1',
'8.2',
'8.1'
]

steps:
Expand Down
1 change: 0 additions & 1 deletion src/CXml/Builder/OrderRequestBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,6 @@ private function buildOrderRequestHeader(): OrderRequestHeader
$this->shipTo,
$this->billTo,
new MoneyWrapper($this->currency, $this->total),
$this->comments,
OrderRequestHeader::TYPE_NEW,
$this->contacts
)
Expand Down
4 changes: 2 additions & 2 deletions src/CXml/Exception/CXmlAuthenticationInvalidException.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

class CXmlAuthenticationInvalidException extends CXmlCredentialInvalidException
{
public function __construct(Credential $credential, \Throwable $previous = null)
public function __construct(Credential $credential)
{
parent::__construct('Given shared secret does not match.', $credential, $previous);
parent::__construct('Given shared secret does not match.', $credential);
}
}
54 changes: 54 additions & 0 deletions src/CXml/Model/CommentsTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace CXml\Model;

use JMS\Serializer\Annotation as Ser;

trait CommentsTrait
{
/**
* @Ser\XmlList(inline=true, entry="Comments")
* @Ser\Type("array<CXml\Model\Comment>")
*
* @var Comment[]
*/
private ?array $comments = null;

public function addCommentAsString(string $comment, string $type = null, string $lang = null): self
{
if (null === $this->comments) {
$this->comments = [];
}

return $this->addComment(new Comment($comment, $type, $lang));
}

public function addComment(Comment $comment): self
{
if (null === $this->comments) {
$this->comments = [];
}

$this->comments[] = $comment;

return $this;
}

public function getComments(): ?array
{
return $this->comments;
}

public function getCommentsAsString(): ?string
{
$commentStrings = [];

if ($comments = $this->getComments()) {
foreach ($comments as $comment) {
$commentStrings[] = $comment->getValue();
}
}

return empty($commentStrings) ? null : \implode("\n", $commentStrings);
}
}
8 changes: 7 additions & 1 deletion src/CXml/Model/Exception/CXmlModelNotFoundException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,10 @@

use CXml\Exception\CXmlNotImplementedException;

class CXmlModelNotFoundException extends CXmlNotImplementedException {}
class CXmlModelNotFoundException extends CXmlNotImplementedException
{
public function __construct(string $xmlNodeName)
{
parent::__construct('Model not found for cXML-node: '.$xmlNodeName);
}
}
32 changes: 32 additions & 0 deletions src/CXml/Model/Message/QuoteMessage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace CXml\Model\Message;

use CXml\Model\MoneyWrapper;
use CXml\Model\OrganizationId;
use JMS\Serializer\Annotation as Ser;

class QuoteMessage implements MessagePayloadInterface
{
/**
* @Ser\SerializedName("QuoteMessageHeader")
*/
private QuoteMessageHeader $quoteMessageHeader;

private function __construct(QuoteMessageHeader $quoteMessageHeader)
{
$this->quoteMessageHeader = $quoteMessageHeader;
}

public static function create(OrganizationId $organizationId, MoneyWrapper $total, string $type, string $quoteId, \DateTime $quoteDate, string $lang = 'en'): self
{
return new self(
new QuoteMessageHeader($organizationId, $total, $type, $quoteId, $quoteDate, $total->getMoney()->getCurrency(), $lang)
);
}

public function getQuoteMessageHeader(): QuoteMessageHeader
{
return $this->quoteMessageHeader;
}
}
108 changes: 108 additions & 0 deletions src/CXml/Model/Message/QuoteMessageHeader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?php

namespace CXml\Model\Message;

use Assert\Assertion;
use CXml\Model\CommentsTrait;
use CXml\Model\Contact;
use CXml\Model\ExtrinsicsTrait;
use CXml\Model\MoneyWrapper;
use CXml\Model\OrganizationId;
use CXml\Model\ShipTo;
use JMS\Serializer\Annotation as Ser;

class QuoteMessageHeader
{
use CommentsTrait;
use ExtrinsicsTrait;

public const TYPE_ACCEPT = 'accept';
public const TYPE_REJECT = 'reject';
public const TYPE_UPDATE = 'update';
public const TYPE_FINAL = 'final';
public const TYPE_AWARD = 'award';

/**
* @Ser\SerializedName("type")
* @Ser\XmlAttribute
*/
private string $type;

/**
* @Ser\SerializedName("quoteID")
* @Ser\XmlAttribute
*/
private string $quoteId;

/**
* @Ser\XmlAttribute
*/
private \DateTimeInterface $quoteDate;

/**
* @Ser\XmlAttribute
*/
private string $currency;

/**
* @Ser\XmlAttribute(namespace="http://www.w3.org/XML/1998/namespace")
*/
private string $lang;

/**
* @Ser\SerializedName("OrganizationID")
* @Ser\XmlElement (cdata=false)
*/
private OrganizationId $organizationId;

/**
* @Ser\SerializedName("Total")
* @Ser\XmlElement (cdata=false)
*/
private MoneyWrapper $total;

/**
* @Ser\SerializedName("ShipTo")
* @Ser\XmlElement (cdata=false)
*/
private ShipTo $shipTo;

/**
* @Ser\SerializedName("Contact")
* @Ser\XmlElement (cdata=false)
*/
private Contact $contact;

public function __construct(OrganizationId $organizationId, MoneyWrapper $total, string $type, string $quoteId, \DateTime $quoteDate, string $currency, string $lang = 'en')
{
Assertion::inArray($type, [
self::TYPE_ACCEPT,
self::TYPE_REJECT,
self::TYPE_UPDATE,
self::TYPE_FINAL,
self::TYPE_AWARD,
]);

$this->organizationId = $organizationId;
$this->total = $total;
$this->type = $type;
$this->quoteId = $quoteId;
$this->quoteDate = $quoteDate;
$this->currency = $currency;
$this->lang = $lang;
}

public function setShipTo(ShipTo $shipTo): self
{
$this->shipTo = $shipTo;

return $this;
}

public function setContact(Contact $contact): self
{
$this->contact = $contact;

return $this;
}
}
23 changes: 23 additions & 0 deletions src/CXml/Model/OrganizationId.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace CXml\Model;

use JMS\Serializer\Annotation as Ser;

class OrganizationId
{
/**
* @Ser\SerializedName("Credential")
*/
private Credential $credential;

public function __construct(Credential $credential)
{
$this->credential = $credential;
}

public function getCredential(): Credential
{
return $this->credential;
}
}
19 changes: 3 additions & 16 deletions src/CXml/Model/Request/OrderRequestHeader.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Assert\Assertion;
use CXml\Model\BillTo;
use CXml\Model\Comment;
use CXml\Model\CommentsTrait;
use CXml\Model\Contact;
use CXml\Model\ExtrinsicsTrait;
use CXml\Model\IdReferencesTrait;
Expand All @@ -19,6 +19,7 @@ class OrderRequestHeader
{
use ExtrinsicsTrait;
use IdReferencesTrait;
use CommentsTrait;

public const TYPE_NEW = 'new';

Expand Down Expand Up @@ -77,14 +78,6 @@ class OrderRequestHeader
*/
private ?array $contacts = null;

/**
* @Ser\XmlList(inline=true, entry="Comments")
* @Ser\Type("array<CXml\Model\Comment>")
*
* @var Comment[]
*/
private ?array $comments = null;

/**
* @Ser\SerializedName("SupplierOrderInfo")
*/
Expand All @@ -96,13 +89,9 @@ public function __construct(
?ShipTo $shipTo,
BillTo $billTo,
MoneyWrapper $total,
array $comments = null,
string $type = self::TYPE_NEW,
array $contacts = null
) {
if ($comments) {
Assertion::allIsInstanceOf($comments, Comment::class);
}
if ($contacts) {
Assertion::allIsInstanceOf($contacts, Contact::class);
}
Expand All @@ -113,7 +102,6 @@ public function __construct(
$this->total = $total;
$this->shipTo = $shipTo;
$this->billTo = $billTo;
$this->comments = $comments;
$this->contacts = $contacts;
}

Expand All @@ -123,11 +111,10 @@ public static function create(
?ShipTo $shipTo,
BillTo $billTo,
MoneyWrapper $total,
array $comments = null,
string $type = self::TYPE_NEW,
array $contacts = null
): self {
return new self($orderId, $orderDate, $shipTo, $billTo, $total, $comments, $type, $contacts);
return new self($orderId, $orderDate, $shipTo, $billTo, $total, $type, $contacts);
}

public function getShipping(): ?Shipping
Expand Down
27 changes: 2 additions & 25 deletions src/CXml/Model/Request/ShipNoticeHeader.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace CXml\Model\Request;

use CXml\Model\Comment;
use CXml\Model\CommentsTrait;
use CXml\Model\DocumentReference;
use CXml\Model\ExtrinsicsTrait;
use CXml\Model\IdReferencesTrait;
Expand All @@ -12,6 +12,7 @@ class ShipNoticeHeader
{
use ExtrinsicsTrait;
use IdReferencesTrait;
use CommentsTrait;

/**
* @Ser\XmlAttribute
Expand Down Expand Up @@ -39,14 +40,6 @@ class ShipNoticeHeader
*/
private ?DocumentReference $documentReference = null;

/**
* @Ser\XmlList(inline=true, entry="Comments")
* @Ser\Type("array<CXml\Model\Comment>")
*
* @var Comment[]
*/
private ?array $comments = null;

public function __construct(string $shipmentId, \DateTimeInterface $noticeDate = null, \DateTimeInterface $shipmentDate = null, \DateTimeInterface $deliveryDate = null, string $documentReference = null)
{
$this->shipmentId = $shipmentId;
Expand All @@ -66,22 +59,6 @@ public function getDocumentReference(): ?DocumentReference
return $this->documentReference;
}

public function addComment(string $comment, string $type = null, string $lang = null): self
{
if (null === $this->comments) {
$this->comments = [];
}

$this->comments[] = new Comment($comment, $type, $lang);

return $this;
}

public function getComments(): ?array
{
return $this->comments;
}

public function getShipmentId(): string
{
return $this->shipmentId;
Expand Down
10 changes: 1 addition & 9 deletions src/CXml/Model/Request/ShipNoticeRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,6 @@ public function getShipNoticePortions(): array

public function getCommentsAsString(): ?string
{
$commentStrings = [];

if ($comments = $this->shipNoticeHeader->getComments()) {
foreach ($comments as $comment) {
$commentStrings[] = $comment->getValue();
}
}

return empty($commentStrings) ? null : \implode("\n", $commentStrings);
return $this->shipNoticeHeader->getCommentsAsString();
}
}
Loading

0 comments on commit 6c21d10

Please sign in to comment.