File Coverage

blib/lib/SilverGoldBull/API/OrderRole.pm
Criterion Covered Total %
statement 7 9 77.7
branch n/a
condition n/a
subroutine 3 3 100.0
pod n/a
total 10 12 83.3


line stmt bran cond sub pod time code
1             package SilverGoldBull::API::OrderRole;
2              
3 1     1   942 use strict;
  1         1  
  1         28  
4 1     1   3 use warnings;
  1         2  
  1         20  
5              
6 1     1   168 use Mouse::Role;
  0            
  0            
7             use Mouse::Util::TypeConstraints;
8             use Scalar::Util qw(blessed);
9              
10             use SilverGoldBull::API::ShippingAddress;
11             use SilverGoldBull::API::BillingAddress;
12             use SilverGoldBull::API::Item;
13              
14             enum 'Declaration' => qw(TEST PROMISE_TO_PAY);
15              
16             has 'currency' => ( is => 'rw', isa => 'Str', required => 1 );
17             has 'declaration' => ( is => 'rw', isa => 'Declaration', required => 1 );
18             has 'payment_method' => ( is => 'rw', isa => 'Str', required => 1 );
19             has 'shipping_method' => ( is => 'rw', isa => 'Str', required => 1 );
20             has 'items' => ( is => 'rw', isa => 'ArrayRef[HashRef]|ArrayRef[SilverGoldBull::API::Item]', required => 1 );
21             has 'shipping' => ( is => 'rw', isa => 'HashRef|Maybe[SilverGoldBull::API::ShippingAddress]', required => 1 );
22             has 'billing' => ( is => 'rw', isa => 'HashRef|Maybe[SilverGoldBull::API::BillingAddress]', required => 1 );
23              
24             sub BUILD {
25             my ($self) = @_;
26            
27             if ($self->shipping && (ref($self->shipping) eq 'HASH')) {
28             my $shipping_obj = SilverGoldBull::API::ShippingAddress->new($self->shipping());
29             $self->shipping($shipping_obj);
30             }
31            
32             if ($self->billing && (ref($self->billing) eq 'HASH')) {
33             my $billing_obj = SilverGoldBull::API::BillingAddress->new($self->billing());
34             $self->billing($billing_obj);
35             }
36            
37             if ($self->items && (ref($self->items) eq 'ARRAY')) {
38             my $items = [];
39             for my $item(@{$self->items}) {
40             my $item_obj = $item;
41             if (ref($item) eq 'HASH') {
42             $item_obj = SilverGoldBull::API::Item->new($item);
43             }
44            
45             push @{$items}, $item_obj;
46             }
47            
48             $self->items($items);
49             }
50             }
51              
52             sub to_hashref {
53             my ($self) = @_;
54             my $hashref = {};
55             for my $field (qw(currency declaration payment_method shipping_method shipping billing)) {
56             if ($self->{$field}) {
57             if (blessed($self->{$field}) && $self->{$field}->can('to_hashref')) {
58             $hashref->{$field} = $self->{$field}->to_hashref();
59             }
60             else {
61             $hashref->{$field} = $self->{$field};
62             }
63             }
64             }
65              
66             $hashref->{items} = [ map { $_->to_hashref() }@{$self->items} ];
67              
68             return $hashref;
69             }
70              
71             1;